/* Kimia UI.
   Hand-written, no framework. Logical properties throughout (inline-start
   rather than left) so the entire interface mirrors correctly for Persian
   without a single RTL-specific rule. */

:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e2e6ec;
  --border-strong: #cbd3dd;
  --text: #16202c;
  --text-dim: #5b6875;
  --text-faint: #8a95a3;
  --accent: #0a7ff2;
  --accent-dim: #e7f1fe;
  --ok: #17914b;
  --ok-dim: #e6f5ec;
  --warn: #b5710a;
  --warn-dim: #fdf3e2;
  --danger: #d02b3a;
  --danger-dim: #fdeaec;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 26, 38, .06), 0 4px 16px rgba(16, 26, 38, .05);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Vazirmatn", Tahoma, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1319;
    --surface: #161d25;
    --surface-2: #1d252f;
    --border: #273241;
    --border-strong: #37455a;
    --text: #e8edf3;
    --text-dim: #9dabbb;
    --text-faint: #6c7c8e;
    --accent: #4da3ff;
    --accent-dim: #14293f;
    --ok: #3ecf7d;
    --ok-dim: #12291d;
    --warn: #e0a340;
    --warn-dim: #2c2312;
    --danger: #ff6b78;
    --danger-dim: #2e1519;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

/* Persian text needs a touch more line height to stay readable. */
[dir="rtl"] body, [dir="rtl"] .msg-body { line-height: 1.75; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

button, input, select, textarea { font: inherit; color: inherit; }

/* ---------------------------------------------------------------- boot --- */

.app-loading { display: grid; place-items: center; height: 100vh; }
.boot { display: grid; gap: 14px; justify-items: center; opacity: .9; }
.boot-mark {
  width: 52px; height: 52px; border-radius: 14px; background: var(--accent);
  color: #fff; display: grid; place-items: center; font-size: 26px; font-weight: 700;
  animation: pulse 1.6s ease-in-out infinite;
}
.boot-text { color: var(--text-dim); letter-spacing: .12em; text-transform: uppercase; font-size: 12px; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .55; } }

/* -------------------------------------------------------------- layout --- */

.shell { display: grid; grid-template-columns: 244px 1fr; height: 100vh; }

.sidebar {
  background: var(--surface);
  border-inline-end: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}

.brand {
  display: flex; align-items: center; gap: 10px;
  padding: 16px 16px 12px; font-weight: 650; font-size: 16px;
}
.brand-mark {
  width: 28px; height: 28px; border-radius: 8px; background: var(--accent);
  color: #fff; display: grid; place-items: center; font-size: 15px; font-weight: 700;
  flex: none;
}

.nav { padding: 6px 8px; display: grid; gap: 2px; }
.nav a {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 10px; border-radius: var(--radius-sm);
  color: var(--text-dim); font-size: 14px; font-weight: 500;
}
.nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav a.active { background: var(--accent-dim); color: var(--accent); font-weight: 600; }

.nav-count {
  background: var(--danger); color: #fff; border-radius: 999px;
  font-size: 11px; font-weight: 700; padding: 1px 7px; min-width: 20px; text-align: center;
}

.sidebar-foot {
  margin-block-start: auto; padding: 12px; border-block-start: 1px solid var(--border);
  display: grid; gap: 8px;
}
.who { font-size: 12.5px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; }
.who strong { color: var(--text); font-weight: 600; }

.main { min-width: 0; min-height: 0; overflow: auto; display: flex; flex-direction: column; }

.page { padding: 22px 26px 40px; max-width: 1180px; width: 100%; }
.page-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 16px; margin-block-end: 18px; flex-wrap: wrap;
}
.page-head h1 { margin: 0 0 3px; font-size: 21px; font-weight: 650; letter-spacing: -.01em; }
.page-head p { margin: 0; color: var(--text-dim); font-size: 13.5px; max-width: 68ch; }

/* --------------------------------------------------------------- parts --- */

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.card + .card { margin-block-start: 14px; }
.card-head {
  padding: 13px 16px; border-block-end: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.card-head h2 { margin: 0; font-size: 14.5px; font-weight: 640; }
.card-body { padding: 16px; }
.card-body.tight { padding: 0; }

.btn {
  appearance: none; border: 1px solid var(--border-strong); background: var(--surface);
  padding: 7px 13px; border-radius: var(--radius-sm); cursor: pointer;
  font-size: 13.5px; font-weight: 550; transition: background .12s, border-color .12s;
  display: inline-flex; align-items: center; gap: 7px; white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-sm { padding: 4px 9px; font-size: 12.5px; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-dim); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }

input[type="text"], input[type="email"], input[type="password"],
input[type="number"], select, textarea {
  width: 100%; padding: 8px 11px; border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); background: var(--surface); outline: none;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-dim);
}
textarea { resize: vertical; min-height: 90px; }
label { display: block; font-size: 12.5px; font-weight: 600; color: var(--text-dim); margin-block-end: 5px; }
.field { margin-block-end: 13px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

.pill {
  display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px;
  border-radius: 999px; font-size: 11.5px; font-weight: 650;
  background: var(--surface-2); color: var(--text-dim); white-space: nowrap;
}
.pill.ok { background: var(--ok-dim); color: var(--ok); }
.pill.warn { background: var(--warn-dim); color: var(--warn); }
.pill.danger { background: var(--danger-dim); color: var(--danger); }
.pill.accent { background: var(--accent-dim); color: var(--accent); }

.empty { text-align: center; padding: 44px 20px; color: var(--text-faint); }
.empty h3 { margin: 0 0 6px; font-size: 15px; color: var(--text-dim); font-weight: 600; }
.empty p { margin: 0 auto; max-width: 46ch; font-size: 13.5px; }

.muted { color: var(--text-dim); }
.faint { color: var(--text-faint); }
.small { font-size: 12.5px; }
.mono { font-family: var(--mono); font-size: 12.5px; }
.nowrap { white-space: nowrap; }

table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { padding: 8px 12px; text-align: start; border-block-end: 1px solid var(--border); }
th { font-weight: 620; color: var(--text-dim); font-size: 12.5px; background: var(--surface-2); position: sticky; top: 0; }
tbody tr:last-child td { border-block-end: none; }
tbody tr:hover { background: var(--surface-2); }
.table-scroll { overflow: auto; max-height: 460px; border-radius: var(--radius); }
td.num, th.num { text-align: end; font-variant-numeric: tabular-nums; }

.progress { height: 5px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--accent); transition: width .3s ease; }

.toast-host {
  position: fixed; inset-block-end: 18px; inset-inline-end: 18px;
  display: grid; gap: 8px; z-index: 90; max-width: 380px;
}
.toast {
  background: var(--surface); border: 1px solid var(--border-strong);
  border-inline-start: 3px solid var(--accent);
  border-radius: var(--radius-sm); box-shadow: var(--shadow);
  padding: 10px 13px; font-size: 13.5px; animation: rise .18s ease;
}
.toast.error { border-inline-start-color: var(--danger); }
.toast.ok { border-inline-start-color: var(--ok); }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }

/* ---------------------------------------------------------------- chat --- */

.chat { display: grid; grid-template-columns: 232px 1fr; height: 100vh; min-height: 0; }
.chat-list { border-inline-end: 1px solid var(--border); background: var(--surface); overflow: auto; }
.chat-list-head { padding: 12px; border-block-end: 1px solid var(--border); }
.conv {
  display: block; padding: 9px 12px; border-block-end: 1px solid var(--border);
  color: var(--text); font-size: 13.5px; cursor: pointer;
}
.conv:hover { background: var(--surface-2); text-decoration: none; }
.conv.active { background: var(--accent-dim); }
.conv-title { font-weight: 560; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.chat-main { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.msgs { flex: 1; overflow: auto; padding: 22px 26px; display: grid; gap: 16px; align-content: start; }
.msg { max-width: 860px; width: 100%; }
.msg-role { font-size: 11.5px; font-weight: 700; color: var(--text-faint); text-transform: uppercase; letter-spacing: .07em; margin-block-end: 5px; }
.msg-body {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 13px 15px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.user .msg-body { background: var(--accent-dim); border-color: transparent; }

.composer { border-block-start: 1px solid var(--border); background: var(--surface); padding: 12px 26px 16px; }
.composer-inner { max-width: 860px; display: grid; gap: 8px; }
.composer textarea { min-height: 52px; max-height: 200px; }

.steps { display: grid; gap: 5px; margin-block-start: 10px; }
.step {
  display: flex; align-items: center; gap: 9px; font-size: 12.5px;
  padding: 5px 10px; background: var(--surface-2); border-radius: var(--radius-sm);
}
.step-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint); flex: none; }
.step-dot.running { background: var(--accent); animation: pulse 1s infinite; }
.step-dot.succeeded { background: var(--ok); }
.step-dot.failed { background: var(--danger); }

.notice {
  border-radius: var(--radius-sm); padding: 9px 12px; font-size: 13px;
  margin-block-start: 9px; border: 1px solid transparent;
}
.notice.warn { background: var(--warn-dim); color: var(--warn); border-color: currentColor; }
.notice.info { background: var(--accent-dim); color: var(--accent); }
.notice ul { margin: 4px 0 0; padding-inline-start: 18px; }

.artifacts { display: flex; flex-wrap: wrap; gap: 8px; margin-block-start: 10px; }
.artifact {
  display: inline-flex; align-items: center; gap: 7px; padding: 7px 12px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); font-size: 13px; font-weight: 550;
}
.artifact:hover { background: var(--surface-2); text-decoration: none; }

.chart { display: grid; gap: 7px; padding: 4px 0; }
.chart-row { display: grid; grid-template-columns: minmax(90px, 22%) 1fr auto; gap: 10px; align-items: center; font-size: 12.5px; }
.chart-bar { background: var(--surface-2); border-radius: 4px; overflow: hidden; height: 17px; }
.chart-bar > i { display: block; height: 100%; background: var(--accent); }

/* --------------------------------------------------------------- misc --- */

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 14px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat-label { font-size: 12px; color: var(--text-dim); font-weight: 600; }
.stat-value { font-size: 23px; font-weight: 660; margin-block-start: 3px; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }

.login-wrap { display: grid; place-items: center; min-height: 100vh; padding: 20px; }
.login-card { width: 100%; max-width: 372px; }

.editor { width: 100%; min-height: 62vh; font-family: var(--mono); font-size: 13px; line-height: 1.6; }

details.diag { margin-block-start: 9px; }
details.diag > summary { cursor: pointer; font-size: 12.5px; color: var(--text-dim); user-select: none; }
details.diag pre {
  background: var(--surface-2); padding: 10px; border-radius: var(--radius-sm);
  overflow: auto; font-size: 12px; margin: 8px 0 0; max-height: 320px;
}

@media (max-width: 880px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: sticky; top: 0; z-index: 20; flex-direction: row;
             align-items: center; gap: 8px; overflow-x: auto;
             border-inline-end: none; border-block-end: 1px solid var(--border); }
  .nav { display: flex; padding: 8px; }
  .sidebar-foot { margin: 0; padding: 8px; border: none; }
  .brand span { display: none; }
  .chat { grid-template-columns: 1fr; }
  .chat-list { display: none; }
  .page { padding: 16px; }
  .msgs, .composer { padding-inline: 16px; }
}

/* ------------------------------------------------------------ context menu */
/* Fixed positioning: the menu is placed from viewport coordinates by
   ui.contextMenu, which measures and flips it near the edges. `dir: ltr` is
   not forced here -- menu text follows the UI language -- but text-align is
   logical so it reads correctly in both directions. */
.ctx-menu {
  position: fixed;
  z-index: 1000;
  min-width: 208px;
  padding: 5px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 1px;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.ctx-item {
  appearance: none;
  background: none;
  border: 0;
  border-radius: 6px;
  padding: 8px 10px;
  font: inherit;
  font-size: 13.5px;
  color: var(--text);
  text-align: start;
  cursor: pointer;
  white-space: nowrap;
}
.ctx-item:hover:not([disabled]),
.ctx-item:focus-visible:not([disabled]) { background: var(--surface-2); outline: none; }
.ctx-item[disabled] { color: var(--text-faint); cursor: default; }
.ctx-item.danger { color: var(--danger); }
.ctx-item.danger:hover:not([disabled]),
.ctx-item.danger:focus-visible:not([disabled]) { background: var(--danger-dim); }
.ctx-sep { height: 1px; background: var(--border); margin: 4px 2px; }

/* A row that can be right-clicked should say so on hover. */
tr.has-menu:hover { background: var(--surface-2); }
