* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --brand: #163bd2;
  --brand-dark: #0f2aa0;
  --brand-soft: #4a68e0;
  --brand-light: #e9edfc;
  --brand-faint: #f3f5fe;
  --ink: #14182b;
  --page-1: #eef1fb;
  --page-2: #e3e9fa;
  --surface: #ffffff;
  --panel: #f7f8fc;
  --border: #e7eaf3;
  --text: #1c2237;
  --muted: #7a819a;
  --success: #0e7d4d;
  --warning: #a05e03;
  --danger: #b3261e;
  --content-max: 880px;
  --admin-max: 1100px;
  --tap-size: 44px;
}

html, body { height: 100%; max-width: 100%; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  font-family: "Alexandria", -apple-system, Tahoma, sans-serif;
  font-weight: 400;
  background:
    radial-gradient(900px 500px at 85% -10%, #dfe7fd 0%, transparent 60%),
    radial-gradient(800px 500px at 10% 110%, #e6ddf9 0%, transparent 55%),
    linear-gradient(160deg, var(--page-1), var(--page-2));
  color: var(--text);
  font-size: 14.5px;
  min-height: 100dvh;
  overflow-x: hidden;
}

.hidden { display: none !important; }

h1, h2, h3, b, strong { font-weight: 500; }

button { font-family: inherit; cursor: pointer; font-weight: 400; touch-action: manipulation; }
input, textarea, select, button { min-width: 0; }

.spacer { flex: 1; }

/* أيقونات ماتيريال — Rounded، وزن 300، Grade -25 */
.ms {
  font-family: "Material Symbols Rounded";
  font-weight: normal;
  font-style: normal;
  font-size: 21px;
  line-height: 1;
  display: inline-block;
  vertical-align: middle;
  direction: ltr;
  font-variation-settings: 'FILL' 0, 'wght' 300, 'GRAD' -25, 'opsz' 48;
  -webkit-font-smoothing: antialiased;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--muted);
  border-radius: 10px;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover { background: var(--panel); color: var(--text); }

.brand { font-weight: 500; font-size: 16px; color: var(--ink); letter-spacing: -0.2px; }
.brand span { color: var(--brand); }
.brand.big { font-size: 25px; }
.brand.ai { color: var(--brand); direction: ltr; letter-spacing: 0.5px; }

.logo-sm { width: 14px; height: 22px; fill: var(--brand); flex-shrink: 0; }

/* ---------- الدخول ---------- */

#login-view {
  height: 100vh;
  min-height: 100dvh;
  height: 100dvh;
  display: flex;
  padding: 28px;
  overflow: auto;
  background: #f4f6fe;
}

.login-shell {
  width: min(1040px, 100%);
  min-height: min(690px, calc(100dvh - 56px));
  margin: auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(360px, 0.9fr);
  overflow: hidden;
  border: 1px solid #e1e6f6;
  border-radius: 28px;
  background: var(--surface);
  box-shadow: 0 28px 80px rgba(24, 40, 100, 0.14);
}

.login-brand-panel {
  padding: 64px;
  background: var(--brand);
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
}

.login-brand-panel .brand { color: #fff; margin-top: 24px; font-size: 34px; }
.login-brand-panel p { max-width: 290px; margin-top: 14px; color: #dce4ff; font-size: 15px; line-height: 1.9; font-weight: 300; }

.login-orb {
  width: 78px;
  height: 78px;
  border-radius: 22px;
  background: rgba(255, 255, 255, 0.14);
  border: 1px solid rgba(255, 255, 255, 0.18);
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-logo { width: 29px; height: 46px; fill: #fff; opacity: 0.98; }
.orb {
  width: 104px;
  height: 104px;
  margin: 0 auto 18px;
  border-radius: 50%;
  background: radial-gradient(circle at 32% 28%, #ffffff 0%, #c9d5fb 35%, #7d97ec 75%, #4a68e0 100%);
  box-shadow: 0 14px 34px rgba(22, 59, 210, 0.28), inset 0 -8px 18px rgba(22, 59, 210, 0.25);
  display: flex; align-items: center; justify-content: center;
}
.orb-logo { width: 26px; height: 42px; fill: #fff; opacity: 0.95; }

.login-card {
  padding: 64px clamp(34px, 6vw, 76px);
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: start;
}

.login-card-mark {
  width: 38px;
  height: 38px;
  margin-bottom: 26px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: var(--brand-faint);
}
.login-card-mark .logo-sm { width: 14px; height: 22px; }

.login-card h1 { color: var(--ink); font-size: 25px; font-weight: 500; line-height: 1.4; }
.login-hint { color: var(--muted); margin: 7px 0 30px; font-size: 13px; line-height: 1.8; font-weight: 300; }

#login-form label { display: block; margin: 0 2px 8px; color: var(--text); font-size: 12px; font-weight: 400; }
#login-form input {
  width: 100%;
  padding: 14px 15px;
  margin-bottom: 18px;
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 14px;
  font-family: inherit;
  background: #fbfcff;
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}
#login-form input::placeholder { color: #a5acc1; }
#login-form input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(22, 59, 210, 0.1); }

#login-form button {
  width: 100%;
  padding: 14px;
  background: var(--brand);
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s, transform 0.15s;
}

#login-form button:hover { background: var(--brand-dark); transform: translateY(-1px); }
#login-form button:disabled { opacity: 0.6; }

.error { color: var(--danger); font-size: 13.5px; margin-top: 12px; }

/* ---------- الإطار العائم ---------- */

#app-view { min-height: 100dvh; padding: 14px; }

#frame {
  display: flex;
  height: calc(100vh - 28px);
  height: calc(100dvh - 28px);
  width: 100%;
  max-width: 1500px;
  margin: 0 auto;
  background: var(--surface);
  border-radius: 22px;
  overflow: hidden;
  box-shadow: 0 24px 80px rgba(20, 30, 80, 0.12);
}

/* ---------- الشريط الجانبي ---------- */

#sidebar {
  width: 268px;
  background: var(--panel);
  border-inline-end: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 14px;
  gap: 13px;
  flex-shrink: 0;
  min-height: 0;
}

#app-view.side-collapsed #sidebar { display: none; }

.side-head { display: flex; align-items: center; }
.side-head .brand-btn { flex: 1; }

.brand-btn { display: flex; align-items: center; gap: 8px; background: none; border: none; padding: 4px; }

#sidebar-close { display: none; }

#new-chat-btn {
  display: flex; align-items: center; justify-content: center; gap: 7px;
  background: var(--ink);
  color: #fff;
  border: none;
  border-radius: 13px;
  padding: 11px;
  font-size: 13.5px;
  font-weight: 500;
}

#new-chat-btn .ms { font-size: 18px; }

.side-nav-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 11px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 12.5px;
  text-align: start;
}

.side-nav-btn:hover { border-color: var(--brand-soft); color: var(--brand-dark); }
.side-nav-btn .ms { color: var(--brand); font-size: 18px; }
.count-badge {
  margin-inline-start: auto;
  min-width: 21px;
  height: 21px;
  padding: 0 6px;
  border-radius: 99px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #fdf0dc;
  color: var(--warning);
  font-size: 10.5px;
  font-weight: 500;
}

.side-section { min-height: 0; }
.side-section.grow { flex: 1; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; }

.side-title {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--muted); font-weight: 500; margin-bottom: 6px;
}

.side-title .ms { font-size: 16px; }

.side-item {
  display: flex; align-items: center; gap: 6px;
  width: 100%;
  text-align: start;
  background: none;
  border: none;
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 12.5px;
  font-weight: 300;
  color: var(--text);
  overflow: hidden;
}

.side-item span:first-child { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.side-item:hover { background: #eef0f7; }
.side-item.active { background: var(--brand-light); color: var(--brand-dark); font-weight: 400; }

#history-list .side-item.conv {
  min-height: 42px;
  padding-block: 10px;
  font-size: 11.5px;
  line-height: 1.55;
  overflow: visible;
}

#history-list .side-item.conv:not(:last-child) {
  margin-bottom: 4px;
  border-bottom: 1px solid rgba(93, 102, 132, 0.12);
}

.side-item .del { color: var(--muted); font-size: 14px; padding: 2px; border: none; background: none; }
.side-item .del:hover { color: var(--danger); }

.side-day { font-size: 11px; color: var(--muted); margin: 9px 2px 3px; font-weight: 300; }

.side-foot { border-top: 1px solid var(--border); padding-top: 12px; display: flex; align-items: center; gap: 6px; }

.me {
  display: flex; align-items: center; gap: 9px; flex: 1; min-width: 0;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 13px; padding: 8px 10px;
}

.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--brand-light); color: var(--brand-dark);
  display: flex; align-items: center; justify-content: center;
  font-weight: 500; font-size: 13px; flex-shrink: 0;
}

.me-info { min-width: 0; }
#me-name { font-size: 12.5px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#me-role { font-size: 11px; color: var(--muted); font-weight: 300; }

/* ---------- المنطقة الرئيسية ---------- */

main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--surface); }

#topbar {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 10px 14px 0;
}

/* بالكمبيوتر يظهر فقط عند طي الشريط الجانبي (لزر الاستعادة) */
#app-view.side-collapsed #topbar { display: flex; }

#menu-btn { display: none; }

#content {
  flex: 1;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

/* ---------- الرئيسية ---------- */

#home-view { width: 100%; max-width: var(--content-max); margin: 0 auto; padding: 20px 22px 12px; }

.hero { text-align: center; padding: 44px 0 30px; }

.hero h1 {
  font-size: 25px;
  font-weight: 300;
  color: var(--brand-soft);
  margin-bottom: 4px;
}

.hero h2 { font-size: 29px; font-weight: 500; color: var(--ink); }

.metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 11px;
  margin-bottom: 12px;
}

.metric {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 13px 16px;
}

.metric .label { font-size: 12px; color: var(--muted); font-weight: 300; }
.metric .value { font-size: 22px; font-weight: 500; margin-top: 3px; }
.metric .value small { font-size: 11.5px; color: var(--muted); font-weight: 300; }
.metric .sub { font-size: 11.5px; color: var(--muted); margin-top: 2px; font-weight: 300; }

.metric.success .sub { color: var(--success); }
.metric.danger .sub { color: var(--danger); }
.metric.brand .value { color: var(--brand); }
.metric.danger .value { color: var(--danger); }

.cards {
  display: grid;
  grid-template-columns: minmax(0, 1.3fr) minmax(250px, 0.85fr);
  grid-template-areas: "ranking promos";
  gap: 11px;
  padding-bottom: 16px;
  align-items: start;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  text-align: start;
  font-family: inherit;
  color: var(--text);
  transition: border-color 0.15s, transform 0.15s;
}

button.card:hover { border-color: var(--brand-soft); transform: translateY(-1px); }

.card-building-store { grid-area: ranking; }
.card-activity { grid-area: activity; }
.card-tag { grid-area: promos; }

.card .card-icon {
  width: 38px; height: 38px;
  border-radius: 11px;
  background: var(--brand-faint);
  color: var(--brand);
  display: flex; align-items: center; justify-content: center;
  margin-bottom: 10px;
}

.card.warning .card-icon { background: #fdf3e3; color: var(--warning); }
.card.danger .card-icon { background: #fdeceb; color: var(--danger); }

.card .card-title { font-size: 13.5px; font-weight: 500; margin-bottom: 7px; color: var(--ink); }

.card .item { font-size: 12.5px; padding: 2.5px 0; font-weight: 300; }
.card .item .isub { color: var(--muted); font-size: 11.5px; }

.card-building-store .item {
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}

.card-building-store .item:last-child {
  padding-bottom: 0;
  border-bottom: none;
}

/* ---------- المحادثة ---------- */

#messages {
  width: 100%;
  max-width: var(--content-max);
  min-width: 0;
  margin: 0 auto;
  padding: 18px 22px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  min-width: 0;
  max-width: 88%;
  padding: 11px 16px;
  border-radius: 18px;
  line-height: 1.9;
  word-break: break-word;
  overflow-wrap: anywhere;
  font-size: 13.5px;
}

.msg.user {
  align-self: flex-start;
  background: var(--brand);
  color: #fff;
  border-bottom-right-radius: 6px;
  white-space: pre-wrap;
  font-weight: 300;
}

.msg.assistant {
  align-self: flex-end;
  background: var(--panel);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
  max-width: 96%;
  font-weight: 300;
}

.msg.assistant p { margin: 2px 0; }
.msg.assistant b { font-weight: 500; }
.msg.assistant a { color: var(--brand); font-weight: 400; }

.msg.typing { color: var(--muted); }
.msg.typing::after { content: "…"; animation: blink 1.2s infinite; }
@keyframes blink { 50% { opacity: 0.2; } }

.table-wrap {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  margin: 8px 0;
  border-radius: 12px;
  overscroll-behavior-inline: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}

.table-wrap:focus-visible { outline: 2px solid var(--brand-soft); outline-offset: 2px; }

.msg.assistant table {
  border-collapse: collapse;
  width: 100%;
  font-size: 12.5px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  min-width: 620px;
  table-layout: auto;
}

.msg.assistant th {
  background: var(--brand-light);
  color: var(--brand-dark);
  font-weight: 500;
  padding: 8px 11px;
  text-align: right;
  white-space: nowrap;
}

.msg.assistant td { border-top: 1px solid var(--border); padding: 8px 11px; text-align: right; font-weight: 300; }
.msg.assistant th, .msg.assistant td { white-space: nowrap; }
.msg.assistant th:first-child, .msg.assistant td:first-child { min-width: 175px; white-space: normal; }
.msg.assistant tbody tr:nth-child(even) { background: #fafbfe; }

/* ---------- مركز المعرفة ---------- */

#admin-view { width: 100%; max-width: var(--admin-max); margin: 0 auto; padding: 34px 30px 50px; }

.admin-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 22px;
}

.admin-heading h1 { font-size: 25px; color: var(--ink); }
.admin-heading p, .admin-form p, .memory-toolbar p {
  color: var(--muted);
  font-size: 12.5px;
  font-weight: 300;
  margin-top: 5px;
  line-height: 1.8;
}

.secondary-btn, .primary-btn, .small-btn, .text-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: 10px;
  font-size: 12px;
  font-family: inherit;
}

.secondary-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 9px 13px;
}

.secondary-btn .ms, .primary-btn .ms, .small-btn .ms { font-size: 17px; }
.secondary-btn:disabled { opacity: .55; }

.knowledge-stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 20px;
}

.knowledge-stat {
  display: flex;
  align-items: center;
  gap: 11px;
  min-height: 78px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 15px;
  background: var(--surface);
}

.knowledge-stat > .ms {
  width: 37px;
  height: 37px;
  display: grid;
  place-items: center;
  border-radius: 11px;
  background: var(--brand-faint);
  color: var(--brand);
}

.knowledge-stat.warning > .ms { color: var(--warning); background: #fdf3e3; }
.knowledge-stat.success > .ms { color: var(--success); background: #eaf7f1; }
.knowledge-stat.muted > .ms { color: var(--muted); background: var(--panel); }
.knowledge-stat div { display: flex; flex-direction: column; gap: 1px; }
.knowledge-stat strong { font-size: 20px; line-height: 1.3; }
.knowledge-stat span:last-child { font-size: 11.5px; color: var(--muted); font-weight: 300; }

.admin-tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 18px;
}

.admin-tab {
  padding: 10px 15px;
  border: none;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--muted);
  font-size: 13px;
}

.admin-tab.active { color: var(--brand-dark); border-bottom-color: var(--brand); font-weight: 500; }

.admin-form {
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 20px;
  background: var(--panel);
  margin-bottom: 24px;
}

.form-title-row, .list-heading, .memory-toolbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.form-title-row h2, .list-heading h2, .memory-toolbar h2 { font-size: 15px; color: var(--ink); }
.form-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 13px;
  margin-top: 17px;
}

.field { display: flex; flex-direction: column; gap: 7px; color: var(--text); font-size: 11.5px; }
.field.span-2 { grid-column: span 2; }
.field input, .field select, .field textarea, #memory-filter,
.memory-controls select, .memory-text {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 300;
  padding: 10px 11px;
  outline: none;
}

.field textarea { resize: vertical; min-height: 98px; line-height: 1.8; }
.field input:focus, .field select:focus, .field textarea:focus,
.memory-text:focus, .memory-controls select:focus { border-color: var(--brand-soft); box-shadow: 0 0 0 3px rgba(22, 59, 210, .08); }

.form-actions { display: flex; align-items: center; justify-content: flex-end; gap: 12px; margin-top: 16px; }
.form-feedback { color: var(--muted); font-size: 12px; }
.primary-btn { padding: 10px 15px; border: none; background: var(--brand); color: #fff; font-weight: 500; }
.primary-btn:hover { background: var(--brand-dark); }
.primary-btn:disabled { opacity: .6; }
.text-btn { padding: 5px; border: none; background: none; color: var(--brand); }

.list-heading { align-items: baseline; margin: 0 3px 10px; }
.list-heading span { color: var(--muted); font-size: 11.5px; }
.admin-list { display: flex; flex-direction: column; gap: 9px; }

.admin-row {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 16px 17px;
  border: 1px solid var(--border);
  border-radius: 15px;
  background: var(--surface);
}

.admin-row.archived, .admin-row.rejected { background: #fbfbfd; opacity: .78; }
.row-main, .memory-content { flex: 1; min-width: 0; }
.row-title-line { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.row-title-line h3 { font-size: 13.5px; }
.directive-copy { margin: 8px 0; font-size: 12.5px; line-height: 1.8; font-weight: 300; white-space: pre-wrap; overflow-wrap: anywhere; }

.status, .version, .confidence {
  display: inline-flex;
  align-items: center;
  border-radius: 99px;
  padding: 4px 8px;
  font-size: 10px;
  white-space: nowrap;
}

.status.active, .status.approved { color: var(--success); background: #eaf7f1; }
.status.pending { color: var(--warning); background: #fdf3e3; }
.status.archived, .status.rejected, .version, .confidence { color: var(--muted); background: var(--panel); }

.row-meta, .source-box {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 7px 14px;
  color: var(--muted);
  font-size: 10.5px;
  font-weight: 300;
}

.row-meta span, .source-box > span { display: inline-flex; align-items: center; gap: 4px; }
.row-meta .ms, .source-box .ms { font-size: 14px; }
.row-actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; justify-content: flex-end; max-width: 240px; }
.small-btn { padding: 7px 9px; border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.small-btn:hover { color: var(--brand-dark); border-color: var(--brand-soft); }
.small-btn.danger { color: var(--danger); }
.empty-state { padding: 34px 20px; text-align: center; color: var(--muted); border: 1px dashed var(--border); border-radius: 14px; font-size: 12.5px; }

.memory-toolbar { align-items: center; margin-bottom: 13px; }
#memory-filter { width: auto; min-width: 170px; padding: 8px 10px; }
.memory-text { resize: vertical; min-height: 64px; line-height: 1.8; }
.memory-controls { display: flex; align-items: flex-end; flex-wrap: wrap; gap: 8px; margin: 9px 0; }
.memory-controls label { display: flex; flex-direction: column; gap: 4px; font-size: 10px; color: var(--muted); }
.memory-controls select { width: auto; min-width: 105px; padding: 6px 8px; font-size: 11px; }
.source-box q { flex-basis: 100%; min-width: 0; line-height: 1.7; color: #686f86; overflow-wrap: anywhere; }

/* ---------- الإدخال ---------- */

footer { flex-shrink: 0; padding: 6px 18px calc(14px + env(safe-area-inset-bottom)); }

.input-card {
  width: 100%;
  max-width: var(--content-max);
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 12px 14px 10px;
  box-shadow: 0 10px 34px rgba(20, 30, 80, 0.07);
}

.input-card:focus-within {
  border-color: var(--border);
  box-shadow: 0 12px 36px rgba(20, 30, 80, 0.09);
}

#chat-input {
  width: 100%;
  border: none;
  outline: none;
  padding: 4px 4px 10px;
  font-size: 14.5px;
  font-family: inherit;
  font-weight: 300;
  resize: none;
  max-height: 170px;
  background: none;
  color: var(--text);
}

.input-row { display: flex; align-items: center; gap: 4px; min-width: 0; }

#send-btn {
  background: var(--brand);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

#send-btn .ms { font-size: 20px; }
#send-btn:disabled { opacity: 0.5; }

#chips { display: flex; gap: 7px; flex-wrap: wrap; margin-inline-start: 6px; }

.chip {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 99px;
  padding: 6px 13px;
  font-size: 12px;
  font-weight: 300;
  color: var(--muted);
}

.chip:hover { border-color: var(--brand-soft); color: var(--brand-dark); background: var(--brand-faint); }

#image-chip {
  display: flex; align-items: center; gap: 8px;
  max-width: 880px; margin: 0 auto 8px;
  background: var(--brand-light);
  border-radius: 12px;
  padding: 6px 10px;
  font-size: 12.5px;
  color: var(--brand-dark);
  width: fit-content;
  max-width: min(var(--content-max), calc(100vw - 32px));
  overflow-wrap: anywhere;
}

#image-chip img { width: 34px; height: 34px; object-fit: cover; border-radius: 8px; }

/* الشاشات المتوسطة مع بقاء الشريط الجانبي */
@media (min-width: 901px) and (max-width: 1100px) {
  #home-view { padding-inline: 18px; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .cards { grid-template-columns: 1fr; grid-template-areas: none; }
  .card-building-store, .card-activity, .card-tag { grid-area: auto; }

  #admin-view { padding: 26px 20px 42px; }
  .knowledge-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .field.span-2 { grid-column: span 2; }
  .admin-row { align-items: stretch; flex-direction: column; gap: 12px; }
  .row-actions { max-width: none; justify-content: flex-start; }
}

/* ---------- الجوال ---------- */

@media (max-width: 900px) {
  #login-view { padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); }
  .login-shell { min-height: auto; grid-template-columns: 1fr; }
  .login-brand-panel { padding: 36px 30px; min-height: 220px; }
  .login-brand-panel .brand { margin-top: 18px; font-size: 28px; }
  .login-brand-panel p { margin-top: 8px; font-size: 13px; }
  .login-orb { width: 62px; height: 62px; border-radius: 18px; }
  .login-logo { width: 24px; height: 38px; }
  .login-card { padding: 36px 28px 42px; }
  .login-card-mark { margin-bottom: 18px; }
  .login-card h1 { font-size: 22px; }

  #app-view { padding: 0; height: 100vh; height: 100dvh; }
  #frame { height: 100vh; height: 100dvh; border-radius: 0; max-width: none; box-shadow: none; }

  .icon-btn { width: var(--tap-size); height: var(--tap-size); }
  #menu-btn { display: inline-flex; }
  #collapse-btn { display: none; }

  #sidebar {
    position: fixed;
    right: 0;
    top: 0;
    height: 100vh;
    height: 100dvh;
    z-index: 30;
    transform: translateX(100%);
    transition: transform 0.22s ease;
    width: min(300px, calc(100vw - 24px));
    padding: max(14px, env(safe-area-inset-top)) 14px max(14px, env(safe-area-inset-bottom));
    box-shadow: 0 0 34px rgba(0,0,0,0.16);
    border-radius: 0;
  }

  #app-view.sidebar-open #sidebar { transform: translateX(0); }
  #app-view.side-collapsed #sidebar { display: flex; }

  #sidebar-overlay {
    position: fixed; inset: 0;
    background: rgba(15, 22, 45, 0.38);
    z-index: 25;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.22s;
  }

  #app-view.sidebar-open #sidebar-overlay { opacity: 1; pointer-events: auto; }
  #sidebar-close { display: inline-flex; }
  #expand-btn { display: none !important; }

  #topbar { display: flex; min-height: calc(48px + env(safe-area-inset-top)); padding: calc(4px + env(safe-area-inset-top)) 8px 0; flex-shrink: 0; }
  #home-view { padding: 8px 14px; }
  .hero { padding: 18px 0 20px; }
  .hero h1 { font-size: 20px; }
  .hero h2 { font-size: 21px; }
  .metrics { grid-template-columns: repeat(auto-fit, minmax(125px, 1fr)); gap: 8px; }
  .metric .value { font-size: 19px; }
  .cards { grid-template-columns: 1fr; grid-template-areas: none; }
  .card-building-store, .card-activity, .card-tag { grid-area: auto; }
  #chips { display: none; }
  footer { padding: 6px max(12px, env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); }

  #admin-view { padding: 16px 14px 36px; }
  .admin-heading { align-items: center; }
  .admin-heading h1 { font-size: 20px; }
  .admin-heading p { max-width: 270px; }
  .knowledge-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .knowledge-stat { min-height: 68px; padding: 11px; }
  .knowledge-stat strong { font-size: 18px; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .field.span-2 { grid-column: span 2; }
  .admin-row { align-items: stretch; flex-direction: column; gap: 12px; }
  .row-actions { max-width: none; justify-content: flex-start; }

  .side-item .del { width: 36px; height: 36px; display: inline-grid; place-items: center; flex-shrink: 0; }
  #home-btn { min-height: 44px; }
  #new-chat-btn, .side-nav-btn { min-height: 44px; }
}

@media (max-width: 600px) {
  .login-brand-panel { min-height: 150px; padding: 24px 24px; }
  .login-brand-panel .brand { margin-top: 12px; font-size: 24px; }
  .login-brand-panel p { margin-top: 5px; font-size: 12px; line-height: 1.6; }
  .login-orb { width: 52px; height: 52px; border-radius: 16px; }
  .login-logo { width: 20px; height: 32px; }
  .login-card { padding: 26px 22px 32px; }
  .login-card-mark { margin-bottom: 14px; }
  .login-card h1 { font-size: 21px; }
  .login-hint { margin: 5px 0 20px; font-size: 12px; }
  #login-form input { margin-bottom: 14px; font-size: 16px; }

  #home-view { padding-inline: 12px; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric { padding: 12px; }
  .card { padding: 14px; }

  #messages { padding: 12px 10px; gap: 10px; }
  .msg { max-width: 92%; padding: 10px 13px; font-size: 13px; line-height: 1.8; }
  .msg.assistant { max-width: 100%; }
  .msg.assistant:has(.table-wrap) { width: 100%; }
  .table-wrap { overflow: visible; border-radius: 0; }
  .msg.assistant table {
    display: block;
    min-width: 0;
    border: none;
    border-radius: 0;
    background: transparent;
    font-size: 12px;
  }
  .msg.assistant thead {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }
  .msg.assistant tbody { display: grid; gap: 9px; }
  .msg.assistant tbody tr {
    display: block;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
  }
  .msg.assistant tbody tr:nth-child(even) { background: var(--surface); }
  .msg.assistant td,
  .msg.assistant td:first-child {
    display: grid;
    grid-template-columns: minmax(78px, .65fr) minmax(0, 1.35fr);
    gap: 10px;
    min-width: 0;
    padding: 8px 10px;
    border-top: 1px solid var(--border);
    white-space: normal;
    text-align: start;
  }
  .msg.assistant td:first-child { border-top: none; }
  .msg.assistant td::before {
    content: attr(data-label);
    color: var(--brand-dark);
    font-weight: 500;
  }

  .input-card { border-radius: 17px; padding: 10px 11px 8px; }
  #chat-input { font-size: 16px; }
  #send-btn { width: 42px; height: 42px; }

  .knowledge-stats { grid-template-columns: 1fr 1fr; }
  .knowledge-stat > .ms { display: none; }
  .form-grid { grid-template-columns: 1fr; }
  .field.span-2 { grid-column: span 1; }
  .admin-form { padding: 15px; }
  .memory-toolbar { align-items: stretch; flex-direction: column; }
  #memory-filter { width: 100%; }
  .admin-heading p { display: none; }
  .secondary-btn { padding: 8px; }
  .admin-heading { gap: 10px; }
  .admin-tabs { gap: 0; }
  .admin-tab { flex: 1; padding-inline: 8px; }
  .form-title-row, .list-heading { flex-wrap: wrap; }
  .form-actions { align-items: stretch; flex-direction: column; }
  .form-feedback:empty { display: none; }
  .primary-btn { min-height: 44px; width: 100%; }
  .row-actions { width: 100%; }
  .row-actions .small-btn:not(.danger) { flex: 1; min-height: 40px; }
  .row-actions .small-btn.danger { width: 40px; height: 40px; }
  .field input, .field select, .field textarea, #memory-filter,
  .memory-controls select, .memory-text { font-size: 16px; }
  .memory-controls { align-items: stretch; }
  .memory-controls label { flex: 1 1 130px; }
  .memory-controls select { width: 100%; }
}

/* الأجهزة الأفقية واللابتوبات القصيرة */
@media (max-height: 600px) and (min-width: 601px) {
  .login-shell {
    min-height: calc(100dvh - 24px);
    grid-template-columns: minmax(190px, .72fr) minmax(330px, 1fr);
  }
  #login-view { padding: 12px; }
  .login-brand-panel { min-height: 0; padding: 26px; }
  .login-brand-panel .brand { margin-top: 14px; font-size: 26px; }
  .login-brand-panel p { margin-top: 7px; font-size: 12px; line-height: 1.6; }
  .login-orb { width: 56px; height: 56px; border-radius: 17px; }
  .login-logo { width: 21px; height: 34px; }
  .login-card { padding: 20px 30px; }
  .login-card-mark { margin-bottom: 10px; }
  .login-card h1 { font-size: 20px; }
  .login-hint { margin: 3px 0 13px; font-size: 11.5px; }
  #login-form label { margin-bottom: 5px; }
  #login-form input { padding: 10px 12px; margin-bottom: 10px; }
  #login-form button { padding: 11px; }

  .hero { padding-block: 8px 12px; }
  .hero h1 { font-size: 18px; }
  .hero h2 { font-size: 20px; }
  #topbar { min-height: 44px; }
  footer { padding-top: 3px; padding-bottom: calc(6px + env(safe-area-inset-bottom)); }
  .input-card { padding-block: 7px 6px; }
  #chat-input { max-height: 82px; padding-bottom: 5px; }
}

@media (max-width: 360px) {
  .hero h1 { font-size: 18px; }
  .hero h2 { font-size: 19px; }
  .metric .value { font-size: 18px; }
  #admin-view { padding-inline: 10px; }
  .admin-heading h1 { font-size: 18px; }
  .knowledge-stat { padding: 10px; }
  .admin-form { padding: 13px; }
  .msg.assistant td, .msg.assistant td:first-child { grid-template-columns: 70px minmax(0, 1fr); gap: 7px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
