:root {
  --bg: #f4f6f9; --surface: #ffffff; --surface-2: #eef1f5; --text: #1b2430; --muted: #5d6b7c;
  --border: #d9dee6; --accent: #1f5fbf; --accent-strong: #174a96; --accent-soft: #e3ecfa;
  --warn: #9a5b00; --warn-soft: #fdf0d9; --danger: #b3261e; --danger-soft: #fde7e5;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161d; --surface: #182029; --surface-2: #212b36; --text: #e4e9ef; --muted: #93a1b1;
    --border: #2e3a47; --accent: #6ea2f2; --accent-strong: #8db6f5; --accent-soft: #1d3150;
    --warn: #f0b860; --warn-soft: #3a2c14; --danger: #f2a39d; --danger-soft: #45201d;
    --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: 14.5px/1.55 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

/* Buttons */
.btn-primary { background: var(--accent); color: #fff; border: 1px solid var(--accent); border-radius: 8px; padding: 8px 18px; font-weight: 600; cursor: pointer; white-space: nowrap; }
.btn-primary:hover { background: var(--accent-strong); }
.btn-primary:disabled { opacity: .6; cursor: wait; }
.btn-ghost { background: none; border: 1px solid var(--border); border-radius: 8px; padding: 5px 12px; cursor: pointer; color: var(--text); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-chip { font-size: 13px; border: 1px solid var(--accent); background: var(--accent-soft); color: var(--accent); border-radius: 7px; padding: 4px 10px; cursor: pointer; margin: 2px 4px 2px 0; }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 2px; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(1200px 600px at 20% 0%, var(--accent-soft), transparent 60%), var(--bg); }
.login-card { width: min(440px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 34px 30px; box-shadow: var(--shadow); }
.login-card h1 { font-size: 25px; margin: 14px 0 8px; line-height: 1.25; }
.login-card p { color: var(--muted); margin: 0 0 22px; }
.eyebrow { font-size: 11.5px; letter-spacing: .08em; color: var(--muted); font-weight: 600; margin-top: 14px; }
.brand-mark { display: inline-grid; place-items: center; width: 46px; height: 46px; border-radius: 11px; background: var(--accent); color: #fff; font-weight: 800; letter-spacing: .04em; }
.brand-mark.sm { width: 30px; height: 30px; border-radius: 8px; font-size: 11.5px; }
img.brand-mark { background: none; border-radius: 0; display: block; }
img.brand-mark:not(.sm) { width: 52px; height: 52px; }
.btn-google { width: 100%; display: flex; align-items: center; justify-content: center; gap: 10px; padding: 11px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-weight: 600; }
.btn-google:hover { background: var(--surface-2); }
.btn-google:disabled { opacity: .6; cursor: wait; }
.login-status { margin: 16px 0 0 !important; min-height: 22px; font-size: 13.5px; }
.login-status.error { color: var(--danger) !important; }

/* App shell */
.app { display: grid; grid-template-rows: auto 1fr auto; height: 100vh; }
.topbar { display: flex; align-items: center; gap: 18px; padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; white-space: nowrap; }
.tabs { display: flex; gap: 4px; flex: 1; }
.tabs a { text-decoration: none; color: var(--muted); padding: 7px 14px; border-radius: 8px; font-weight: 600; white-space: nowrap; }
.tabs a:hover { background: var(--surface-2); color: var(--text); }
.tabs a.active { background: var(--accent-soft); color: var(--accent); }
.user { display: flex; align-items: center; gap: 10px; min-width: 0; }
.user img { width: 28px; height: 28px; border-radius: 50%; }
.user-email { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.tab-panel { min-height: 0; overflow: hidden; display: flex; flex-direction: column; }
.statusbar { display: flex; justify-content: space-between; gap: 16px; padding: 5px 16px; font-size: 12.5px; background: var(--surface); border-top: 1px solid var(--border); }
.statusbar span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Search */
.searchbar { display: flex; gap: 8px; padding: 12px 16px; }
.searchbar input { flex: 1; min-width: 0; padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.searchbar select, .ask-row select { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); max-width: 300px; }
.search-layout { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(280px, 380px) 1fr; gap: 12px; padding: 0 16px 12px; }
.results { overflow: auto; min-height: 0; }
.result-list { list-style: none; margin: 6px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.result-list li { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 9px 12px; cursor: pointer; }
.result-list li:hover { border-color: var(--accent); }
.result-list li.active { border-color: var(--accent); background: var(--accent-soft); }
.r-code { font-size: 12px; color: var(--accent); font-weight: 600; }
.r-title { font-weight: 600; margin: 1px 0 3px; }
.r-snip { font-size: 13px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.detail { overflow: auto; min-height: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; }
.empty { color: var(--muted); max-width: 640px; margin: 30px auto; text-align: center; }
.empty h3 { color: var(--text); margin: 0 0 8px; }
.std-summary { margin-top: 18px; display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.std-summary span { background: var(--surface-2); border-radius: 99px; padding: 3px 11px; font-size: 12.5px; color: var(--text); }

/* Hit view */
.hit-code { font-size: 13px; color: var(--accent); font-weight: 700; }
.hit-title { font-size: 18px; margin: 2px 0 2px; line-height: 1.35; }
.hit-meta { font-size: 12.5px; color: var(--muted); }
.hit-tools { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.hit-image { position: relative; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: #fff; min-height: 80px; }
.hit-image canvas { display: block; width: 100%; height: auto; }
.hit-image .loading { position: absolute; inset: 0; display: grid; place-items: center; color: #5d6b7c; font-size: 13px; background: rgba(255, 255, 255, .7); }
.hit-text { margin-top: 12px; }
.hit-text summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.hit-text pre { white-space: pre-wrap; font: 13.5px/1.55 inherit; font-family: inherit; background: var(--surface-2); border-radius: 8px; padding: 10px 12px; margin: 6px 0 0; }
.hit-text mark { background: #ffe58a; color: #1b2430; border-radius: 2px; }

/* Shared question + attachments */
.sync-row { padding: 0 16px 8px; margin-top: -4px; }
.ask-box .sync-row { padding: 0; margin: 0; }
.attach-list { display: flex; flex-wrap: wrap; gap: 8px; }
.attach-item { position: relative; width: 112px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); overflow: hidden; }
.attach-item img { display: block; width: 100%; height: 76px; object-fit: cover; background: #fff; }
.attach-item span { display: block; font-size: 11px; color: var(--muted); padding: 2px 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attach-item button { position: absolute; top: 3px; right: 3px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; cursor: pointer; line-height: 22px; padding: 0; font-size: 12px; }
.ask-box.drag { outline: 2px dashed var(--accent); outline-offset: 4px; border-radius: 10px; }
.sent-images { display: flex; flex-wrap: wrap; gap: 6px; margin: 12px 0 0; }
.sent-images img { height: 64px; border: 1px solid var(--border); border-radius: 6px; cursor: zoom-in; background: #fff; }
.reading { margin: 12px 0 0; font-size: 13.5px; }
.reading summary { cursor: pointer; color: var(--muted); }
.reading pre { white-space: pre-wrap; font-family: inherit; background: var(--surface-2); border-radius: 8px; padding: 10px 12px; margin: 6px 0 0; }
.badge-deep { display: inline-block; font-size: 11.5px; font-weight: 600; color: var(--accent); background: var(--accent-soft); border-radius: 99px; padding: 1px 9px; margin-top: 12px; }

/* AI */
.ask-box { padding: 12px 16px; display: flex; flex-direction: column; gap: 8px; max-width: 980px; width: 100%; margin: 0 auto; }
.ask-box textarea { width: 100%; resize: vertical; min-height: 70px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
.ask-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.ask-row .btn-primary { margin-left: auto; }
.check { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.answer { flex: 1; overflow: auto; min-height: 0; width: 100%; max-width: 980px; margin: 0 auto 12px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 26px 30px; }
.answer h2 { font-size: 17px; margin: 22px 0 6px; padding-bottom: 4px; border-bottom: 1px solid var(--border); }
.answer table { border-collapse: collapse; display: block; overflow-x: auto; }
.answer th, .answer td { border: 1px solid var(--border); padding: 4px 8px; }
.answer code { background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }
.answer .katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }
a.cite { display: inline-block; font-size: 12px; font-weight: 600; color: var(--accent); background: var(--accent-soft); border-radius: 5px; padding: 0 5px; margin: 0 1px; text-decoration: none; cursor: pointer; }
a.cite.bad { color: var(--danger); background: var(--danger-soft); cursor: default; }
.notice { background: var(--warn-soft); color: var(--warn); border-radius: 8px; padding: 8px 12px; margin: 12px 0; }
.notice.error { background: var(--danger-soft); color: var(--danger); }
.refs { margin-top: 20px; font-size: 13.5px; }
.refs li { margin: 3px 0; }
.meta { color: var(--muted); font-size: 12.5px; margin-top: 24px; border-top: 1px solid var(--border); padding-top: 8px; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--accent-soft); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; margin: 0 auto 12px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Drawer */
.drawer { position: fixed; inset: 0; z-index: 50; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(10, 15, 22, .45); }
.drawer-panel { position: absolute; top: 0; right: 0; bottom: 0; width: min(760px, 100%); background: var(--surface); padding: 18px 20px; overflow: auto; box-shadow: var(--shadow); }
.drawer-close { position: sticky; top: 0; float: right; z-index: 2; background: var(--surface); }

/* Knowledge */
.kb-layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 270px 1fr; gap: 12px; padding: 12px 16px; }
.kb-side { overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; }
.kb-side input { width: 100%; padding: 7px 9px; border: 1px solid var(--border); border-radius: 7px; margin-bottom: 8px; background: var(--surface); }
.kb-side .cat { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 10px 8px 3px; }
.kb-side button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 7px 9px; border-radius: 7px; cursor: pointer; }
.kb-side button:hover { background: var(--surface-2); }
.kb-side button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.kb-article { overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 26px 40px; }
.kb-article h2 { margin: 4px 0 10px; }
.kb-article .meta { margin-top: 0; border: 0; padding: 0; }
.kb-article .pill { display: inline-block; font-size: 11px; padding: 1px 8px; border-radius: 99px; background: var(--warn-soft); color: var(--warn); }
.kb-article table { border-collapse: collapse; margin: 8px 0; display: block; overflow-x: auto; }
.kb-article th, .kb-article td { border: 1px solid var(--border); padding: 4px 9px; text-align: center; }
.kb-article th { background: var(--surface-2); }
.kb-article .formula { font-family: "Cambria Math", Cambria, serif; background: var(--surface-2); padding: 7px 12px; border-radius: 7px; margin: 6px 0; overflow-x: auto; }
.kb-article .hint { color: var(--muted); font-size: 13px; }
.kb-article .refs { margin-top: 14px; }
.kb-article .btn { font-size: 13px; border: 1px solid var(--accent); background: var(--accent-soft); color: var(--accent); border-radius: 7px; padding: 4px 10px; cursor: pointer; margin: 2px 4px 2px 0; }
.kb-article .calc { background: var(--surface-2); border-radius: 10px; padding: 12px 14px; margin-top: 16px; }
.kb-article .calc h4 { margin: 0 0 8px; }
.kb-article .calc .grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.kb-article .calc label { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 3px; }
.kb-article .calc input, .kb-article .calc select { padding: 5px 7px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); }
.kb-article .calc .out { margin-top: 10px; white-space: pre-wrap; font-family: Consolas, monospace; font-size: 13px; }

/* Phone / narrow */
@media (max-width: 860px) {
  .app { height: auto; min-height: 100vh; }
  .topbar { flex-wrap: wrap; gap: 8px 12px; padding: 8px 16px; }
  .brand-text { display: none; }
  .tabs { order: 3; flex-basis: 100%; overflow-x: auto; }
  .user { margin-left: auto; }
  .user-email { max-width: 140px; }
  .tab-panel { overflow: visible; }
  .searchbar { flex-wrap: wrap; }
  .searchbar input { flex-basis: 100%; }
  .searchbar select { flex: 1; max-width: none; }
  .search-layout, .kb-layout { grid-template-columns: 1fr; }
  .results, .detail, .kb-side, .kb-article, .answer { overflow: visible; }
  .kb-side { max-height: 260px; overflow: auto; }
  .answer { padding: 6px 16px 24px; margin: 0 0 12px; }
  .ask-box { padding: 12px 16px; }
  .ask-row select { flex: 1; max-width: none; }
  .ask-row .btn-primary { width: 100%; }
  .statusbar { flex-direction: column; gap: 2px; }
  .statusbar span { white-space: normal; }
}
