:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --ink: #1b1e33;
  --muted: #5d6178;
  --line: #e1e4ef;
  --navy: #241a4f;
  --teal: #6b4fd8;
  --mint: #c9bdff;
  --amber: #ff9f6e;
  --accent: #241a4f;
  --accent-ink: #ffffff;
  --bar: #6b4fd8;
  --good: #1f8a5c;
  --bad: #c0392b;
  --bad-bg: #fdecea;
  --warn-bg: #fff4e2;
  --field: #ffffff;
  --shadow: 0 1px 2px rgba(27, 30, 51, .05), 0 6px 18px rgba(27, 30, 51, .06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12142a;
    --card: #1c1f3a;
    --ink: #eef0fa;
    --muted: #a7abc6;
    --line: #2d3156;
    --teal: #a996ff;
    --accent: #ff9f6e;
    --accent-ink: #12142a;
    --bar: #a996ff;
    --good: #5ccf98;
    --bad: #ff8a7a;
    --bad-bg: #3a1f2a;
    --warn-bg: #33291a;
    --field: #15182f;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.5 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
.wrap { max-width: 1100px; margin-inline: auto; padding-inline: 16px; }
a { color: var(--teal); }
h2 { font-size: 1.35rem; margin: 8px 0 12px; }
h3 { font-size: 1.05rem; margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: .88rem; }

/* Header */
.hero { background: var(--navy); color: #fff; margin-bottom: 18px; }
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-top: 16px; padding-bottom: 12px; }
.brand { display: flex; align-items: center; gap: 12px; }
.brand img { border-radius: 12px; }
h1 { font-size: 1.3rem; margin: 0; line-height: 1.2; }
.brand p { margin: 0; color: var(--mint); font-size: .9rem; }
.badge { background: var(--amber); color: #12142a; font-weight: 700; font-size: .8rem; padding: 4px 10px; border-radius: 999px; white-space: nowrap; }
.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; padding-bottom: 0; }
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  color: #d9dcf0; text-decoration: none; padding: 10px 12px; border-radius: 10px 10px 0 0;
  white-space: nowrap; font-weight: 600; font-size: .93rem;
}
.tabs a:hover { background: rgba(255, 255, 255, .08); }
.tabs a[aria-current="page"] { background: var(--bg); color: var(--ink); }

/* Cards & layout */
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 16px; box-shadow: var(--shadow); margin-bottom: 16px; }
.grid { display: grid; gap: 12px; }
.kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 760px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .two, .three { grid-template-columns: 1fr; }
}
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px; box-shadow: var(--shadow); }
.kpi span { display: block; color: var(--muted); font-size: .85rem; }
.kpi strong { font-size: 1.6rem; font-variant-numeric: tabular-nums; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spread { justify-content: space-between; }

/* Buttons & fields */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 16px; border-radius: 10px; font-weight: 600; font-size: .95rem; font-family: inherit;
  text-decoration: none; border: 1px solid var(--line); color: var(--ink); background: var(--card); cursor: pointer;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.small { padding: 6px 10px; font-size: .85rem; }
label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 12px; }
label .hint { font-weight: 400; color: var(--muted); }
input, select, textarea {
  display: block; width: 100%; margin-top: 4px; padding: 10px 12px; font: inherit; color: var(--ink);
  background: var(--field); border: 1px solid var(--line); border-radius: 10px;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible, .tabs a:focus-visible { outline: 2px solid var(--teal); outline-offset: 1px; }
textarea { min-height: 110px; resize: vertical; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 400; }
.check input { width: auto; margin: 0; }

/* Messages */
.msg { border-radius: 10px; padding: 10px 14px; margin: 0 0 14px; }
.msg.error { background: var(--bad-bg); color: var(--bad); }
.msg.warn { background: var(--warn-bg); }
.msg.info { background: var(--card); border: 1px solid var(--line); }
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 10;
  background: var(--navy); color: #fff; padding: 10px 18px; border-radius: 999px; box-shadow: 0 6px 18px rgba(0, 0, 0, .25);
  max-width: calc(100% - 32px);
}

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .93rem; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: .82rem; text-transform: uppercase; letter-spacing: .02em; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pill { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .8rem; font-weight: 600; background: var(--line); }
.pill.on { background: rgba(31, 138, 92, .15); color: var(--good); }


/* Welcome */
.welcome { max-width: 680px; margin: 10px auto 30px; }
.welcome ol { padding-left: 20px; }
.welcome li { margin-bottom: 6px; }
.loading { color: var(--muted); padding: 30px 0; text-align: center; }
.foot { color: var(--muted); font-size: .85rem; padding-top: 10px; padding-bottom: 30px; }
.tabs a.locked::after { content: " 🔒"; font-size: .8em; }
code { background: var(--line); border-radius: 5px; padding: 1px 5px; font-size: .9em; }

/* Tool cards */
.tools { grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.tool {
  display: block; text-decoration: none; color: var(--ink); background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 14px 16px; box-shadow: var(--shadow); position: relative;
}
.tool:hover { border-color: var(--teal); }
.tool .ic { font-size: 1.5rem; }
.tool strong { display: block; margin: 4px 0 2px; }
.tool p { margin: 0; color: var(--muted); font-size: .88rem; }
.tool .lock { position: absolute; top: 10px; right: 12px; font-size: .78rem; color: var(--muted); }
.cat { margin: 18px 0 8px; font-size: .82rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }

/* Work area: form + output */
.work { display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); gap: 16px; align-items: start; }
@media (max-width: 860px) { .work { grid-template-columns: 1fr; } }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.chip { border: 1px solid var(--line); background: var(--field); color: var(--ink); border-radius: 999px; padding: 5px 12px; font: inherit; font-size: .85rem; cursor: pointer; }
.chip:hover { border-color: var(--teal); }
.chip:disabled { opacity: .5; cursor: not-allowed; }

/* Rendered AI text */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { font-size: 1.05rem; margin: 16px 0 6px; }
.md h1 { font-size: 1.2rem; }
.md p { margin: 0 0 10px; }
.md ul, .md ol { margin: 0 0 10px; padding-left: 22px; }
.md li { margin-bottom: 3px; }
.md table { margin: 6px 0 12px; }
.md .table-wrap { margin-bottom: 10px; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 14px 0; }
.md blockquote { margin: 0 0 10px; padding: 4px 12px; border-left: 3px solid var(--teal); color: var(--muted); }
.output { min-height: 160px; }
.output .placeholder { color: var(--muted); }
.cursor::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--teal); }
@keyframes blink { 50% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .cursor::after { animation: none; } }

/* Chat */
.chat { display: grid; grid-template-columns: 240px minmax(0, 1fr); gap: 16px; align-items: start; }
@media (max-width: 760px) { .chat { grid-template-columns: 1fr; } .convs { order: 2; } }
.convs { padding: 10px; }
.convs a { display: block; padding: 8px 10px; border-radius: 8px; color: var(--ink); text-decoration: none; font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.convs a:hover { background: var(--line); }
.convs a[aria-current="true"] { background: var(--line); font-weight: 600; }
.thread { display: flex; flex-direction: column; gap: 12px; min-height: 300px; }
.bubble { border-radius: 14px; padding: 10px 14px; max-width: 100%; }
.bubble.user { align-self: flex-end; background: var(--teal); color: #fff; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble.ai { background: var(--card); border: 1px solid var(--line); }
.bubble .acts { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.composer { display: flex; gap: 8px; align-items: flex-end; margin-top: 14px; }
.composer textarea { min-height: 52px; max-height: 220px; margin: 0; }
.starters { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
@media (max-width: 560px) { .starters { grid-template-columns: 1fr; } }
.starters button { text-align: left; justify-content: flex-start; }

/* Library */
.docs { display: grid; gap: 8px; }
.doc { display: flex; justify-content: space-between; gap: 12px; align-items: center; padding: 12px 14px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; text-decoration: none; color: var(--ink); }
.doc:hover { border-color: var(--teal); }
.doc span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc small { color: var(--muted); white-space: nowrap; }
.meter { height: 8px; background: var(--line); border-radius: 999px; overflow: hidden; margin: 6px 0 2px; }
.meter div { height: 100%; background: var(--teal); }
