:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #f0eee9;
  --text: #1d1c1a;
  --muted: #6d6a63;
  --border: #e3e0d9;
  --accent: #2f5d50;
  --accent-text: #ffffff;
  --user-bubble: #2f5d50;
  --user-text: #ffffff;
  --bot-bubble: #ffffff;
  --old: #a0522d;
  --danger: #b3261e;
  --chip: #e7efe9;
  --chip-text: #2f5d50;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151615;
    --surface: #1e201e;
    --surface-2: #262926;
    --text: #ecebe7;
    --muted: #9c9a93;
    --border: #333632;
    --accent: #7fbfa8;
    --accent-text: #0f1a16;
    --user-bubble: #2c4a40;
    --user-text: #ecebe7;
    --bot-bubble: #1e201e;
    --old: #e0a17a;
    --danger: #f2948c;
    --chip: #253530;
    --chip-text: #9fd6c2;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 "Inter", "Noto Sans Arabic", system-ui, sans-serif;
  display: flex;
  flex-direction: column;
}
[dir="rtl"], :lang(ar) { font-family: "Noto Sans Arabic", "Inter", sans-serif; }
button { font: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--surface);
  flex-wrap: wrap;
}
.brand { font-weight: 600; letter-spacing: 0.04em; display: flex; align-items: center; gap: 8px; }
.logo {
  width: 26px; height: 26px; border-radius: 7px; background: var(--accent); color: var(--accent-text);
  display: inline-grid; place-items: center; font-size: 13px;
}
.tag { font-weight: 400; color: var(--muted); font-size: 12px; letter-spacing: 0; }
.controls { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.field { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.field input {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 6px;
  padding: 5px 8px; width: 130px; font-size: 13px;
}

.layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 240px 1fr 380px; }
.sidebar {
  border-right: 1px solid var(--border); padding: 14px; display: flex; flex-direction: column; gap: 10px;
  min-height: 0; background: var(--surface);
}
.side-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin-top: 6px; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv-list li {
  padding: 8px 10px; border-radius: 7px; cursor: pointer; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; color: var(--text);
}
.conv-list li small { display: block; color: var(--muted); font-size: 11px; }
.conv-list li:hover { background: var(--surface-2); }
.conv-list li.active { background: var(--chip); color: var(--chip-text); }

.primary {
  background: var(--accent); color: var(--accent-text); border: 0; border-radius: 8px; padding: 9px 14px; font-weight: 500;
}
.primary:disabled { opacity: 0.5; cursor: default; }
.ghost { background: transparent; border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; color: var(--text); }
.danger-link { background: none; border: 0; color: var(--danger); font-size: 12px; text-align: left; padding: 4px 0; }

.chat { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.messages { flex: 1; overflow-y: auto; padding: 24px clamp(16px, 4vw, 48px); display: flex; flex-direction: column; gap: 14px; }
.empty { margin: auto; max-width: 440px; text-align: center; color: var(--muted); }
.empty h2 { color: var(--text); font-weight: 600; }
.msg { max-width: min(720px, 85%); padding: 10px 14px; border-radius: 14px; box-shadow: var(--shadow); white-space: normal; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user-bubble); color: var(--user-text); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--bot-bubble); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg .meta { font-size: 11px; opacity: 0.7; margin-top: 4px; }
.msg p { margin: 0 0 6px; } .msg p:last-child { margin: 0; }
.msg ul, .msg ol { margin: 4px 0; padding-inline-start: 20px; }
.msg.pending { color: var(--muted); font-style: italic; }
.private-badge { font-size: 10px; background: rgba(255,255,255,0.2); border-radius: 4px; padding: 1px 5px; margin-left: 6px; }

.composer { display: flex; gap: 8px; padding: 12px clamp(16px, 4vw, 48px) 6px; }
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); background: var(--surface); border-radius: 10px;
  padding: 10px 12px; max-height: 160px; outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.saving { font-size: 12px; color: var(--muted); padding: 0 clamp(16px, 4vw, 48px) 10px; }
.hidden { display: none !important; }

.panel { border-left: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tabs button { flex: 1; background: none; border: 0; padding: 11px 6px; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 500; }
.tab { display: none; overflow-y: auto; padding: 14px; flex: 1; }
.tab.active { display: block; }
.section-title { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 14px 0 6px; }
.section-title:first-child { margin-top: 0; }
.item { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; margin-bottom: 6px; position: relative; background: var(--bg); }
.item .date { font-size: 11px; color: var(--muted); }
.item.old { opacity: 0.75; }
.item.old .text { text-decoration: line-through; text-decoration-color: var(--old); }
.item .why { font-size: 11px; color: var(--old); margin-top: 3px; }
.item .del { position: absolute; top: 4px; right: 6px; background: none; border: 0; color: var(--muted); font-size: 15px; line-height: 1; }
.item .del:hover { color: var(--danger); }
.status { font-size: 11px; padding: 1px 7px; border-radius: 10px; background: var(--chip); color: var(--chip-text); margin-left: 6px; }
.status.paused, .status.dropped { background: rgba(160, 82, 45, 0.15); color: var(--old); }
.story h4 { margin: 0 24px 4px 0; font-size: 14px; }
.story p { margin: 4px 0; }
.timeline { list-style: none; padding: 0; margin: 6px 0 0; font-size: 12px; }
.timeline li { padding-left: 12px; border-left: 2px solid var(--border); margin-left: 3px; padding-bottom: 4px; }
.timeline li b { color: var(--muted); font-weight: 500; }
.kv { font-size: 12px; } .kv b { color: var(--muted); font-weight: 500; }
pre.json { font-size: 11px; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 8px; white-space: pre-wrap; overflow-wrap: anywhere; }
.muted { color: var(--muted); font-size: 12px; }

.only-narrow { display: none; }
@media (max-width: 1100px) {
  .layout { grid-template-columns: 200px 1fr; }
  .panel { display: none; }
  .panel.open { display: flex; position: fixed; inset: 56px 0 0 auto; width: min(380px, 100%); z-index: 5; box-shadow: -4px 0 16px rgba(0,0,0,0.15); }
  .only-narrow { display: inline-block; }
}
@media (max-width: 700px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .field input { width: 110px; }
  .msg { max-width: 92%; }
}

/* ---------------------------------------------------------------- shared additions */
:root { --ok: #2e7d4f; --warn: #a86b00; --bad: #b3261e; --ok-bg: #e3f1e8; --warn-bg: #fbefd9; --bad-bg: #f9e3e1; }
@media (prefers-color-scheme: dark) {
  :root { --ok: #7fd1a0; --warn: #f0c060; --bad: #f2948c; --ok-bg: #1e3327; --warn-bg: #3a2f17; --bad-bg: #3d2120; }
}
.app { display: flex; flex-direction: column; height: 100%; }
.error { color: var(--bad); font-size: 13px; min-height: 1em; margin: 4px 0; }
.error-msg { color: var(--bad); }
.hint, .small { font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.wide { width: 100%; }
button:disabled { opacity: 0.5; cursor: default; }
.danger { color: var(--bad) !important; }
.danger-btn { background: var(--bad) !important; color: #fff !important; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label input {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 9px 11px;
  font-size: 14px; color: var(--text); outline: none;
}
label input:focus { border-color: var(--accent); }

/* ---------------------------------------------------------------- sign in */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card {
  width: min(380px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 28px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 16px;
}
.brand.big { font-size: 18px; justify-content: center; }
.auth-tabs { display: flex; border-bottom: 1px solid var(--border); }
.auth-tabs button { flex: 1; background: none; border: 0; padding: 8px; color: var(--muted); border-bottom: 2px solid transparent; }
.auth-tabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 500; }
#authForm { display: flex; flex-direction: column; gap: 12px; }

/* ---------------------------------------------------------------- top bar: views + menu */
.views { display: flex; gap: 4px; background: var(--surface-2); padding: 3px; border-radius: 9px; }
.views button { background: none; border: 0; padding: 5px 12px; border-radius: 7px; color: var(--muted); }
.views button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 500; }
.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20; min-width: 210px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 6px; box-shadow: 0 8px 24px rgba(0,0,0,0.12);
  display: flex; flex-direction: column;
}
.menu-list button { background: none; border: 0; text-align: left; padding: 8px 10px; border-radius: 6px; color: var(--text); }
.menu-list button:hover { background: var(--surface-2); }
.menu-list hr { border: 0; border-top: 1px solid var(--border); margin: 4px 0; width: 100%; }

dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 22px; width: min(420px, calc(100% - 32px));
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
dialog h3 { margin: 0 0 6px; }
#confirmForm { display: flex; flex-direction: column; gap: 12px; }

/* ---------------------------------------------------------------- test lab */
.lab { overflow-y: auto; padding: 20px clamp(16px, 3vw, 36px) 40px; min-width: 0; }
.lab-head { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; flex-wrap: wrap; }
.lab-head h2 { margin: 0 0 4px; }
.lab-head p { margin: 0; max-width: 640px; }
.lab-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.lab-import { margin: 16px 0; border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; background: var(--surface); }
.lab-import summary { cursor: pointer; font-weight: 500; }
.lab-import textarea {
  width: 100%; min-height: 150px; margin: 10px 0 6px; border: 1px solid var(--border); border-radius: 8px;
  padding: 10px; background: var(--bg); resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px;
}
.lab-summary { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 8px 14px; font-size: 12px; color: var(--muted); }
.tile span { display: block; font-size: 20px; font-weight: 600; color: var(--text); }
.tile.pass span { color: var(--ok); } .tile.partial span { color: var(--warn); } .tile.fail span { color: var(--bad); }
.lab-items { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 10px; }
.lab-item { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--border); border-radius: 10px; padding: 12px 14px; position: relative; }
.lab-item.pass { border-left-color: var(--ok); } .lab-item.partial { border-left-color: var(--warn); }
.lab-item.fail, .lab-item.error { border-left-color: var(--bad); } .lab-item.running { border-left-color: var(--accent); }
.lab-item-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.lab-item-head .num { font-weight: 600; color: var(--muted); min-width: 22px; }
.lab-item .del { position: static; margin-left: auto; background: none; border: 0; color: var(--muted); font-size: 16px; }
.mode { font-size: 11px; border-radius: 10px; padding: 1px 8px; border: 1px solid var(--border); background: var(--surface-2); color: var(--muted); }
.mode.new { background: var(--chip); color: var(--chip-text); border-color: transparent; }
.lab-text { white-space: pre-wrap; }
.lab-expect { width: 100%; margin-top: 6px; border: 1px dashed var(--border); background: transparent; border-radius: 6px; padding: 5px 8px; font-size: 12px; color: var(--text); }
.lab-reply { margin-top: 10px; background: var(--bg); border-radius: 8px; padding: 8px 12px; font-size: 13px; }
.lab-reply p { margin: 0 0 4px; } .lab-reply ul, .lab-reply ol { margin: 2px 0; padding-inline-start: 20px; }
.lab-chips { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-top: 8px; }
.chip { font-size: 11px; padding: 1px 8px; border-radius: 10px; background: var(--surface-2); color: var(--muted); }
.chip.ok { background: var(--ok-bg); color: var(--ok); } .chip.accent { background: var(--warn-bg); color: var(--warn); }
.chip.bad { background: var(--bad-bg); color: var(--bad); }
.lab-mem { font-size: 12px; margin: 6px 0 0; padding-inline-start: 18px; color: var(--muted); }
.lab-mem b { color: var(--text); font-weight: 500; }
.bad-text { color: var(--bad); }
.lab-check { margin-top: 8px; font-size: 13px; padding: 8px 10px; border-radius: 8px; background: var(--surface-2); }
.lab-check.pass { background: var(--ok-bg); } .lab-check.partial { background: var(--warn-bg); } .lab-check.fail { background: var(--bad-bg); }
.lab-check ul { margin: 4px 0 0; padding-inline-start: 18px; }
.badge { font-size: 11px; font-weight: 600; padding: 2px 9px; border-radius: 10px; }
.badge.pass { background: var(--ok-bg); color: var(--ok); } .badge.partial { background: var(--warn-bg); color: var(--warn); }
.badge.fail { background: var(--bad-bg); color: var(--bad); } .badge.running { background: var(--chip); color: var(--chip-text); font-weight: 500; }
