/* IRANYekanX — FaNum cut (Persian numerals by default), licensed webfont.
   Declared here rather than in a separate file so both pages get it for free. */
@font-face { font-family: "IRANYekanX"; src: url("/assets/fonts/IRANYekanXFaNum-Light.woff2") format("woff2");     font-weight: 300; font-display: swap; }
@font-face { font-family: "IRANYekanX"; src: url("/assets/fonts/IRANYekanXFaNum-Regular.woff2") format("woff2");   font-weight: 400; font-display: swap; }
@font-face { font-family: "IRANYekanX"; src: url("/assets/fonts/IRANYekanXFaNum-Medium.woff2") format("woff2");    font-weight: 500; font-display: swap; }
@font-face { font-family: "IRANYekanX"; src: url("/assets/fonts/IRANYekanXFaNum-DemiBold.woff2") format("woff2");  font-weight: 600; font-display: swap; }
@font-face { font-family: "IRANYekanX"; src: url("/assets/fonts/IRANYekanXFaNum-Bold.woff2") format("woff2");      font-weight: 700; font-display: swap; }
@font-face { font-family: "IRANYekanX"; src: url("/assets/fonts/IRANYekanXFaNum-ExtraBold.woff2") format("woff2"); font-weight: 800; font-display: swap; }

/* Theming: light is the base, dark is applied by the system preference.
   An explicit choice (data-theme on <html>) overrides the system in BOTH
   directions, so "روشن" still wins on a dark-mode OS. Default = follow system. */
:root {
  --bg: #f4f6f8; --panel: #ffffff; --ink: #1c2733; --muted: #6b7a89;
  --accent: #0f766e; --accent-ink: #ffffff; --accent-weak: #0f766e14;
  --line: #dde4ea;
  --user: #0f766e12; --bot: #ffffff; --err: #b42318; --warn: #b45309;
  --sidebar-w: 290px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #10161c; --panel: #1a2229; --ink: #e8eef4; --muted: #8fa1b0;
    --accent: #2dd4bf; --accent-ink: #06302b; --accent-weak: #2dd4bf1c;
    --line: #2b3640;
    --user: #2dd4bf14; --bot: #1f2830; --err: #f97066; --warn: #fdb022;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #10161c; --panel: #1a2229; --ink: #e8eef4; --muted: #8fa1b0;
  --accent: #2dd4bf; --accent-ink: #06302b; --accent-weak: #2dd4bf1c;
  --line: #2b3640;
  --user: #2dd4bf14; --bot: #1f2830; --err: #f97066; --warn: #fdb022;
  color-scheme: dark;
}
:root[data-theme="light"] { color-scheme: light; }
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: "IRANYekanX", Vazirmatn, "Iranian Sans", Tahoma, "Segoe UI", sans-serif;
  font-size: 15px; line-height: 1.7;
}
header {
  display: flex; align-items: center; gap: 12px; padding: 12px 20px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
header h1 { font-size: 16px; margin: 0; font-weight: 700; }
header .sub { color: var(--muted); font-size: 12.5px; }
header a { color: var(--accent); text-decoration: none; margin-inline-start: auto; font-size: 13px; }
main { max-width: 880px; margin: 0 auto; padding: 20px 16px 40px; }

.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 16px; margin-bottom: 16px;
}
.card h2 { font-size: 14.5px; margin: 0 0 12px; }
button {
  font: inherit; border: 1px solid var(--line); border-radius: 9px;
  background: var(--panel); color: var(--ink); padding: 7px 14px; cursor: pointer;
}
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button:disabled { opacity: .45; cursor: not-allowed; }
input[type=text], input[type=number], input[type=search], select {
  font: inherit; color: var(--ink); background: var(--bg);
  border: 1px solid var(--line); border-radius: 9px; padding: 7px 10px;
}
input:disabled { opacity: .6; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: right; padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 12.5px; }
.tablewrap { overflow-x: auto; }
.pill {
  display: inline-block; border-radius: 99px; padding: 1px 10px; font-size: 12px;
  background: var(--accent-weak); color: var(--accent);
}
.pill.gray { background: var(--line); color: var(--muted); }
.pill.err { background: #b4231822; color: var(--err); }
.pill.warn { background: #b4530922; color: var(--warn); }
.muted { color: var(--muted); font-size: 12.5px; }
.errtext { color: var(--err); font-size: 13px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 10px 16px; }
.grid label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }

/* ---------- chat shell: sidebar + column (sidebar sits at the RTL start = right) ---------- */
body.chat-page { height: 100vh; overflow: hidden; }
.shell { display: flex; height: 100vh; }
.main-col { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.main-col main { flex: 1; overflow-y: auto; width: 100%; padding-bottom: 20px; }

.sidebar {
  width: var(--sidebar-w); background: var(--panel);
  border-inline-end: 1px solid var(--line);
  display: flex; flex-direction: column; flex-shrink: 0;
  transition: margin-inline-start .22s ease;
}
.sidebar.closed { margin-inline-start: calc(-1 * var(--sidebar-w)); }
.sidebar-head { padding: 14px 16px 8px; }
.brand { font-weight: 800; font-size: 16px; color: var(--accent); }
.brand small { display: block; font-weight: 400; font-size: 11.5px; color: var(--muted); }
.new-chat-btn {
  margin: 8px 14px 6px; padding: 9px 14px; background: var(--accent);
  color: var(--accent-ink); border: none; border-radius: 9px;
  font-size: 13.5px; font-weight: 600;
  display: flex; align-items: center; gap: 8px; justify-content: center;
}
.conv-list { flex: 1; overflow-y: auto; padding: 8px 10px; }
.conv-group-label { font-size: 11px; color: var(--muted); font-weight: 600; padding: 10px 8px 4px; }
.conv-item {
  display: flex; align-items: center; gap: 6px; padding: 8px 10px;
  border-radius: 9px; color: var(--ink); font-size: 13.5px; cursor: pointer;
  border: 1px solid transparent;
}
.conv-item:hover { background: var(--bg); }
.conv-item.active { background: var(--accent-weak); color: var(--accent); border-color: var(--accent); }
.conv-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item .actions { display: none; gap: 2px; }
.conv-item:hover .actions, .conv-item.active .actions { display: flex; }
.icon-btn {
  background: none; border: none; padding: 3px 5px; border-radius: 6px;
  color: var(--muted); font-size: 13px; line-height: 1;
}
.icon-btn:hover { background: var(--line); color: var(--ink); }
.sidebar-foot { border-top: 1px solid var(--line); padding: 10px 14px; }

@media (max-width: 820px) {
  /* overlay instead of squeezing the conversation */
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 20;
             box-shadow: 0 0 30px #0004; }
}

/* ---------- messages ---------- */
.msgs { display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 78%; padding: 10px 14px; border-radius: 14px; border: 1px solid var(--line); }
.msg.user { align-self: flex-start; background: var(--user); border-color: transparent; }
.msg.bot  { align-self: flex-end; background: var(--bot); }
.msg.user.provisional { opacity: .6; font-style: italic; }
.msg .meta { color: var(--muted); font-size: 11.5px; margin-top: 6px; }
.msg .cite { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; }
#txt.listening { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }

/* ---------- citation cards ---------- */
.cite-card {
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
  padding: 8px 10px; font-size: 13px;
}
.cite-card .q { font-weight: 600; cursor: pointer; display: flex; gap: 6px; align-items: flex-start; }
.cite-card .q .n { color: var(--accent); flex-shrink: 0; }
.cite-card .q .chev { margin-inline-start: auto; color: var(--muted); font-size: 11px; transition: transform .18s; }
.cite-card.open .q .chev { transform: rotate(180deg); }
.cite-card .src { color: var(--muted); font-size: 11px; margin-top: 3px; }
.cite-card .body {
  display: none; margin-top: 8px; padding-top: 8px;
  border-top: 1px dashed var(--line); white-space: pre-wrap; line-height: 1.75;
}
.cite-card.open .body { display: block; }

/* ---------- waiting / thinking state ---------- */
.stages { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.stage {
  font-size: 11.5px; color: var(--muted); border: 1px dashed var(--line);
  border-radius: 99px; padding: 1px 9px;
}
.stage.done { border-style: solid; color: var(--accent); border-color: var(--accent); }
.stage.fail { border-style: solid; color: var(--err); border-color: var(--err); }
.stage.active {
  border-style: solid; color: var(--accent); border-color: var(--accent);
  animation: pulse 1.1s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .45; } }

.typing { display: flex; gap: 4px; align-items: center; height: 18px; }
.typing i {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
  display: inline-block; animation: blink 1.2s infinite ease-in-out;
}
.typing i:nth-child(2) { animation-delay: .18s; }
.typing i:nth-child(3) { animation-delay: .36s; }
@keyframes blink { 0%,80%,100% { opacity: .25; transform: translateY(0); }
                   40% { opacity: 1; transform: translateY(-3px); } }

@media (prefers-reduced-motion: reduce) {
  .stage.active, .typing i { animation: none; }
  .sidebar { transition: none; }
}

/* ---------- system alert banner (key exhausted / missing) ---------- */
.alertbar { display: flex; flex-direction: column; gap: 6px; padding: 0; }
.alertbar:not(:empty) { padding: 10px 16px; }
.alert {
  display: flex; gap: 8px; align-items: flex-start; font-size: 13px;
  border: 1px solid var(--warn); border-radius: 10px; padding: 8px 12px;
  background: #b4530914; color: var(--ink);
}
.alert.crit { border-color: var(--err); background: #b4231814; }
.alert .who { font-weight: 700; flex-shrink: 0; }
.alert .what { flex: 1; }
.alert .hint { color: var(--muted); font-size: 12px; }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 380px; margin: 0; }
.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px;
         color: var(--muted); margin-bottom: 12px; }
.field input { font-size: 15px; }

/* whoami row in the sidebar footer */
.whoami { display: flex; align-items: center; gap: 8px; font-size: 12.5px; }
.whoami .avatar {
  width: 28px; height: 28px; border-radius: 50%; background: var(--accent);
  color: var(--accent-ink); display: grid; place-items: center; font-weight: 700;
  flex-shrink: 0;
}
.whoami .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- MVP tag ---------- */
/* Marks the deployment as a prototype. Deliberately legible rather than
   subtle: this is public, and a caller should not mistake it for a finished
   service (answers are not stored as audio, and there is no human handoff). */
.mvp-tag {
  display: inline-block; vertical-align: middle;
  margin-inline-start: 6px; padding: 1px 7px;
  border-radius: 99px; border: 1px solid var(--warn);
  color: var(--warn); background: color-mix(in srgb, var(--warn) 12%, transparent);
  font-size: 10px; font-weight: 700; letter-spacing: .06em;
  font-family: ui-sans-serif, system-ui, sans-serif;
}
header .mvp-tag { margin-inline-start: 8px; }

/* ---------- theme toggle (single cycling icon) ---------- */
.theme-toggle {
  width: 34px; height: 34px; padding: 0; border-radius: 50%;
  display: inline-grid; place-items: center; font-size: 15px; line-height: 1;
  border: 1px solid var(--line); background: var(--panel); cursor: pointer;
  transition: background .15s, border-color .15s, transform .15s;
}
.theme-toggle:hover { background: var(--bg); border-color: var(--accent); transform: scale(1.06); }
.theme-toggle:active { transform: scale(0.94); }
@media (prefers-reduced-motion: reduce) { .theme-toggle { transition: none; } }

/* ---------- pagination ---------- */
.pager { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 10px; }
.pager .count { color: var(--muted); font-size: 12.5px; }
.load-more {
  width: 100%; margin-top: 6px; font-size: 12.5px; color: var(--muted);
  background: transparent; border: 1px dashed var(--line);
}
.load-more:hover:not(:disabled) { color: var(--accent); border-color: var(--accent); }

/* ---------- composer ---------- */
.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 12px 16px; }
.composer .inner { max-width: 880px; margin: 0 auto; display: flex; gap: 10px; align-items: center; }
.composer input { flex: 1; }
/* Record button, after the reference pen: turns red while recording with a
   spinning ring around it (a border whose top edge is transparent). */
#mic {
  position: relative; flex-shrink: 0;
  width: 44px; height: 44px; padding: 0; border-radius: 50%;
  font-size: 18px; display: grid; place-items: center;
  transition: background .3s ease-in-out, color .3s ease-in-out;
}
#mic.rec { background: var(--err); border-color: var(--err); color: #fff; }
#mic::before {
  content: ""; position: absolute; inset: -4px;
  border-radius: 50%; border: 3px solid var(--accent);
  border-top-color: transparent;
  opacity: 0; transition: opacity .3s ease-in-out;
  animation: spin 1.6s linear infinite;
}
#mic.rec::before { opacity: 1; }
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

/* Waveform strip above the composer row. It must NOT overlay the text field:
   that field shows the live interim transcript while recording, and an opaque
   canvas on top of it hid the feature entirely. */
.waverow {
  max-height: 0; overflow: hidden; padding: 0;
  transition: max-height .2s ease, margin-bottom .2s ease;
}
.waverow.on { max-height: 56px; margin-bottom: 8px; }
#wave {
  width: 100%; height: 48px; display: block;
  border-radius: 9px; background: var(--bg); pointer-events: none;
}

button.stop { background: var(--err); border-color: var(--err); color: #fff; font-weight: 600; }
button.stop:hover { filter: brightness(1.08); }

@media (prefers-reduced-motion: reduce) {
  #mic.rec { animation: none; }
  #wave { transition: none; }
}
