/* QueryGym — core styles. Light/dark aware, accessible, responsive. */
:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --border-strong: #cfd4dc;
  --text: #1a1d23;
  --text-dim: #5b616e;
  --text-faint: #8a909c;
  --accent: #2f6df6;
  --accent-ink: #ffffff;
  --accent-soft: #e7eefe;
  --ok: #148a4e;
  --ok-soft: #e3f5ec;
  --bad: #d0352b;
  --bad-soft: #fcebe9;
  --warn: #b7791f;
  --code-bg: #f3f4f7;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.10);
  --shadow-lg: 0 8px 30px rgba(16,24,40,.12);
  --radius: 10px;
  --radius-sm: 7px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Fira Code", Menlo, Consolas, monospace;
  --sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
:root:not([data-theme="light"]) {
  @media (prefers-color-scheme: dark) {
    --bg: #0e1117;
    --surface: #161b22;
    --surface-2: #1c2230;
    --border: #262c38;
    --border-strong: #363d4d;
    --text: #e6e9ef;
    --text-dim: #9aa3b2;
    --text-faint: #6b7483;
    --accent: #5b8dff;
    --accent-ink: #0b1220;
    --accent-soft: #15223d;
    --ok: #3ad17e;
    --ok-soft: #10251a;
    --bad: #ff6b5e;
    --bad-soft: #2a1512;
    --warn: #e0a94a;
    --code-bg: #0b0f16;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    --shadow-lg: 0 10px 40px rgba(0,0,0,.5);
  }
}
:root[data-theme="dark"] {
  --bg: #0e1117;
  --surface: #161b22;
  --surface-2: #1c2230;
  --border: #262c38;
  --border-strong: #363d4d;
  --text: #e6e9ef;
  --text-dim: #9aa3b2;
  --text-faint: #6b7483;
  --accent: #5b8dff;
  --accent-ink: #0b1220;
  --accent-soft: #15223d;
  --ok: #3ad17e;
  --ok-soft: #10251a;
  --bad: #ff6b5e;
  --bad-soft: #2a1512;
  --warn: #e0a94a;
  --code-bg: #0b0f16;
  --shadow: 0 1px 2px rgba(0,0,0,.4);
  --shadow-lg: 0 10px 40px rgba(0,0,0,.5);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  font-size: 15px;
  line-height: 1.5;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font-family: inherit; cursor: pointer; }
h1,h2,h3 { line-height: 1.25; }

/* ---------- App shell ---------- */
.app { display: flex; flex-direction: column; height: 100vh; }
.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 10px 16px; background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 17px; letter-spacing: -.01em; }
.brand .logo {
  width: 26px; height: 26px; border-radius: 7px;
  background: linear-gradient(135deg, var(--accent), #8a5bff);
  display: grid; place-items: center; color: #fff; font-weight: 800; font-size: 14px;
}
.brand small { color: var(--text-faint); font-weight: 500; font-size: 12px; }
.topbar .spacer { flex: 1; }
.stat {
  display: inline-flex; align-items: baseline; gap: 5px;
  padding: 4px 10px; background: var(--surface-2); border-radius: 999px;
  font-size: 13px; color: var(--text-dim); border: 1px solid var(--border);
}
.stat b { color: var(--text); font-variant-numeric: tabular-nums; font-size: 14px; }
.btn {
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  padding: 7px 13px; border-radius: var(--radius-sm); font-size: 13.5px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 7px; transition: .12s;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.07); color: var(--accent-ink); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--text-dim); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.pro { background: linear-gradient(135deg, #8a5bff, var(--accent)); color: #fff; border: none; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { padding: 7px; border-radius: var(--radius-sm); }

.layout { display: flex; flex: 1; min-height: 0; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 320px; flex-shrink: 0; background: var(--surface);
  border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0;
}
.sidebar-head { padding: 12px 14px 8px; border-bottom: 1px solid var(--border); }
.search { position: relative; }
.search input {
  width: 100%; padding: 8px 10px 8px 32px; border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); background: var(--bg); color: var(--text); font-size: 13.5px;
}
.search input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.search .mag { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--text-faint); font-size: 13px; }
.filters { display: flex; gap: 6px; margin-top: 9px; flex-wrap: wrap; }
.chip {
  font-size: 12px; padding: 3px 9px; border-radius: 999px; border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text-dim); font-weight: 600;
}
.chip[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.problem-list { overflow-y: auto; flex: 1; padding: 6px; }
.group-label {
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-faint); padding: 12px 10px 5px;
}
.pitem {
  display: flex; align-items: center; gap: 9px; padding: 9px 10px; border-radius: var(--radius-sm);
  cursor: pointer; border: 1px solid transparent;
}
.pitem:hover { background: var(--surface-2); }
.pitem.active { background: var(--accent-soft); border-color: var(--accent); }
.pitem .pt-title { flex: 1; font-size: 13.5px; font-weight: 550; min-width: 0; }
.pitem .pt-title .cat { display: block; font-size: 11px; color: var(--text-faint); font-weight: 500; }
.pstate { width: 18px; height: 18px; flex-shrink: 0; display: grid; place-items: center; font-size: 12px; }
.pstate.solved { color: var(--ok); }
.pstate.locked { color: var(--text-faint); }
.pstate.due { color: var(--warn); }
.diff-dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }
.diff-easy { background: #22a06b; } .diff-medium { background: #d98b1a; } .diff-hard { background: #d0352b; }

/* ---------- Workspace ---------- */
.workspace { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow: hidden; }
.ws-empty { margin: auto; text-align: center; color: var(--text-faint); max-width: 420px; padding: 40px; }
.ws-empty h2 { color: var(--text-dim); font-weight: 650; }
.prompt-pane { padding: 16px 20px; border-bottom: 1px solid var(--border); overflow-y: auto; max-height: 48%; flex-shrink: 0; }
.prompt-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.prompt-head h2 { margin: 0; font-size: 18px; letter-spacing: -.01em; }
.badge {
  font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px;
  border: 1px solid var(--border-strong); color: var(--text-dim);
}
.badge.easy { color: #22a06b; border-color: #22a06b55; background: #22a06b14; }
.badge.medium { color: var(--warn); border-color: #d98b1a55; background: #d98b1a14; }
.badge.hard { color: var(--bad); border-color: #d0352b55; background: #d0352b14; }
.badge.schema { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); cursor: pointer; }
.prompt-body { margin-top: 10px; color: var(--text); white-space: pre-wrap; }
.prompt-body code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; font-family: var(--mono); font-size: 13px; }
.cols-note { margin-top: 10px; font-size: 13px; color: var(--text-dim); }
.cols-note b { color: var(--text); }
.hint-row { margin-top: 12px; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.hint-box { margin-top: 10px; background: var(--surface-2); border: 1px solid var(--border); border-left: 3px solid var(--warn); padding: 9px 12px; border-radius: var(--radius-sm); font-size: 13.5px; }
.hint-box .hn { font-weight: 700; color: var(--warn); margin-right: 6px; }

/* editor + results */
.editor-pane { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.editor-toolbar { display: flex; align-items: center; gap: 8px; padding: 8px 14px; border-bottom: 1px solid var(--border); background: var(--surface); }
.editor-toolbar .spacer { flex: 1; }
.editor-wrap { position: relative; border-bottom: 1px solid var(--border); }
.CodeMirror { height: 180px !important; font-family: var(--mono) !important; font-size: 13.5px !important; }
.results-pane { flex: 1; overflow: auto; background: var(--bg); min-height: 0; }
.feedback { padding: 10px 14px; font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 9px; position: sticky; top: 0; z-index: 2; }
.feedback.pass { background: var(--ok-soft); color: var(--ok); border-bottom: 1px solid var(--ok); }
.feedback.fail { background: var(--bad-soft); color: var(--bad); border-bottom: 1px solid var(--bad); }
.feedback.err  { background: var(--bad-soft); color: var(--bad); border-bottom: 1px solid var(--bad); font-weight: 500; }
.feedback .big { font-size: 18px; }
.diff-note { font-weight: 500; font-size: 13px; color: var(--text-dim); margin-left: auto; }
.table-scroll { padding: 4px 14px 24px; }
.rtitle { font-size: 12px; color: var(--text-faint); font-weight: 700; text-transform: uppercase; letter-spacing: .05em; margin: 14px 0 6px; }
table.rt { border-collapse: collapse; width: 100%; font-size: 13px; background: var(--surface); border-radius: 8px; overflow: hidden; box-shadow: var(--shadow); }
table.rt th, table.rt td { padding: 6px 11px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.rt th { background: var(--surface-2); font-weight: 700; color: var(--text-dim); position: sticky; top: 0; }
table.rt td { font-variant-numeric: tabular-nums; }
table.rt tr:last-child td { border-bottom: none; }
table.rt td.nullcell { color: var(--text-faint); font-style: italic; }
table.rt tr.row-missing td { background: var(--ok-soft); }
table.rt tr.row-extra td { background: var(--bad-soft); }
.muted { color: var(--text-faint); font-size: 13px; padding: 10px 2px; }

.solution-box { margin: 14px; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.solution-box summary { padding: 10px 14px; background: var(--surface-2); font-weight: 650; cursor: pointer; font-size: 13.5px; }
.solution-box .sol-body { padding: 14px; background: var(--surface); }
.solution-box pre { margin: 0 0 10px; background: var(--code-bg); padding: 12px; border-radius: 8px; overflow-x: auto; font-family: var(--mono); font-size: 13px; }
.solution-box .wt { color: var(--text-dim); font-size: 13.5px; }

/* ---------- Schema modal / Pro modal ---------- */
.overlay { position: fixed; inset: 0; background: rgba(8,12,20,.55); display: none; place-items: center; z-index: 100; padding: 20px; }
.overlay.show { display: grid; }
.modal { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow-lg); max-width: 560px; width: 100%; max-height: 86vh; overflow: auto; }
.modal-head { display: flex; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 17px; flex: 1; }
.modal-body { padding: 18px 20px; }
.modal-body h4 { margin: 16px 0 6px; font-size: 13px; }
.schema-table { margin-bottom: 14px; }
.schema-table .st-name { font-family: var(--mono); font-weight: 700; color: var(--accent); font-size: 13.5px; }
.schema-table .st-cols { font-family: var(--mono); font-size: 12.5px; color: var(--text-dim); margin-top: 3px; }
.price-row { display: flex; gap: 12px; margin: 14px 0; flex-wrap: wrap; }
.price-card { flex: 1; min-width: 180px; border: 1px solid var(--border-strong); border-radius: var(--radius); padding: 16px; }
.price-card.best { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.price-card .amt { font-size: 28px; font-weight: 800; letter-spacing: -.02em; }
.price-card .per { color: var(--text-faint); font-size: 13px; }
.price-card ul { padding-left: 18px; margin: 10px 0 0; color: var(--text-dim); font-size: 13px; }
.key-input { display: flex; gap: 8px; margin-top: 12px; }
.key-input input { flex: 1; padding: 9px 11px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); font-family: var(--mono); background: var(--bg); color: var(--text); text-transform: uppercase; }
.key-msg { font-size: 13px; margin-top: 8px; min-height: 18px; }
.key-msg.ok { color: var(--ok); } .key-msg.bad { color: var(--bad); }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-size: 13.5px; font-weight: 600; opacity: 0; transition: .25s; z-index: 200; box-shadow: var(--shadow-lg); }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.loading-screen { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); z-index: 500; }
.spinner { width: 34px; height: 34px; border: 3px solid var(--border-strong); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Brief (clear task inputs) ---------- */
.brief { margin-top: 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); padding: 10px 14px; font-size: 13.5px; }
.brief-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--accent); margin-bottom: 6px; }
.brief-row { display: flex; gap: 10px; padding: 4px 0; align-items: baseline; }
.brief-k { flex: 0 0 110px; color: var(--text-faint); font-weight: 600; font-size: 12.5px; }
.brief-v { flex: 1; min-width: 0; color: var(--text); line-height: 1.7; }
.brief code, .hint-box code, .solution-box .wt code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; font-family: var(--mono); font-size: 12.5px; }
.tbl-chip { font: inherit; font-family: var(--mono); font-size: 12px; color: var(--text-dim); background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 1px 7px; margin: 0 4px 4px 0; cursor: pointer; text-align: left; }
.tbl-chip b { color: var(--accent); }
.tbl-chip:hover { border-color: var(--accent); }
.brief-notes { display: flex; gap: 10px; padding: 4px 0; }
.brief-notes ul { flex: 1; margin: 0; padding-left: 18px; }
.brief-notes li { margin: 2px 0; }
.brief-sample { margin-top: 6px; }
.brief-sample summary { cursor: pointer; color: var(--text-dim); font-size: 12.5px; font-weight: 600; padding: 4px 0; }
.brief-sample .table-scroll { padding: 6px 0 4px; }

/* ---------- Language switch ---------- */
.lang-switch { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 999px; overflow: hidden; flex-shrink: 0; }
.lang-switch button { border: none; background: transparent; color: var(--text-dim); font-size: 12px; font-weight: 700; padding: 4px 9px; }
.lang-switch button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }

/* ---------- Account ---------- */
.account { display: flex; align-items: center; }
.account-menu { position: relative; }
.account-btn { padding: 4px 8px; }
.avatar { width: 26px; height: 26px; border-radius: 50%; object-fit: cover; flex-shrink: 0; }
.avatar-letter { display: grid; place-items: center; background: linear-gradient(135deg, var(--accent), #8a5bff); color: #fff; font-weight: 700; font-size: 13px; }
.account-pop { position: absolute; right: 0; top: calc(100% + 6px); min-width: 220px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg); padding: 6px; z-index: 60; }
.account-who { display: flex; flex-direction: column; padding: 8px 10px 10px; border-bottom: 1px solid var(--border); margin-bottom: 4px; font-size: 13px; }
.account-who span { color: var(--text-faint); font-size: 12px; overflow: hidden; text-overflow: ellipsis; }
.account-pop button { display: block; width: 100%; text-align: left; background: none; border: none; color: var(--text); padding: 8px 10px; border-radius: var(--radius-sm); font-size: 13.5px; }
.account-pop button:hover { background: var(--surface-2); }
.account-pop button.danger { color: var(--bad); }
.auth-modal { max-width: 420px; }
.auth-text { margin-top: 0; color: var(--text-dim); }
.auth-buttons { display: flex; flex-direction: column; align-items: center; gap: 10px; margin: 18px 0 8px; }
.google-btn { min-height: 44px; display: flex; align-items: center; justify-content: center; }
.apple-btn { width: 280px; height: 44px; border-radius: 999px; border: none; background: #000; color: #fff; font-size: 15px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
:root[data-theme="dark"] .apple-btn { background: #fff; color: #000; }
.apple-btn span::before { content: "\f8ff"; font-family: -apple-system, BlinkMacSystemFont, sans-serif; }
.auth-msg { min-height: 18px; color: var(--bad); font-size: 13px; text-align: center; }
.auth-legal { color: var(--text-faint); font-size: 12px; text-align: center; margin-bottom: 0; }

/* ---------- AI tutor ---------- */
.btn.ai { border-color: #8a5bff; color: #8a5bff; }
.btn.ai:hover { background: #8a5bff14; color: #8a5bff; border-color: #8a5bff; }
.tutor { width: 400px; flex-shrink: 0; display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-left: 1px solid var(--border); }
.tutor[hidden] { display: none; }
.tutor-head { display: flex; align-items: center; gap: 6px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.tutor-head .spacer { flex: 1; }
.tutor-title { font-weight: 700; font-size: 14.5px; display: inline-flex; align-items: center; gap: 6px; }
.tutor-beta { text-transform: lowercase; }
.tutor-body { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; min-height: 0; }
.tutor-empty, .tutor-gate { color: var(--text-dim); font-size: 13.5px; margin: auto 0; text-align: center; padding: 10px; }
.tutor-gate p { margin-top: 0; }
.tmsg { display: flex; }
.tmsg.user { justify-content: flex-end; }
.tmsg-body { max-width: 92%; padding: 9px 12px; border-radius: 12px; font-size: 13.5px; line-height: 1.55; overflow-wrap: anywhere; }
.tmsg.user .tmsg-body { background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.tmsg.ai .tmsg-body { background: var(--surface-2); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.tmsg.ai.err .tmsg-body { border-color: var(--bad); }
.tmsg-body p { margin: 0 0 8px; } .tmsg-body p:last-child { margin-bottom: 0; }
.tmsg-body ul, .tmsg-body ol { margin: 4px 0 8px; padding-left: 20px; }
.tmsg-body code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; font-family: var(--mono); font-size: 12.5px; }
.tmsg-note { color: var(--text-faint); font-size: 12px; margin-top: 6px; }
.tmsg.ai.err .tmsg-note { color: var(--bad); }
.tutor-code { position: relative; margin: 6px 0 8px; }
.tutor-code pre { margin: 0; background: var(--code-bg); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; overflow-x: auto; }
.tutor-code pre code { background: none; padding: 0; font-size: 12.5px; }
.tutor-copy { position: absolute; top: 5px; right: 5px; font-size: 11px; border: 1px solid var(--border-strong); background: var(--surface); color: var(--text-dim); border-radius: 5px; padding: 1px 7px; }
.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--text-faint); animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.tutor-quick { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 12px 8px; }
.tutor-quick .chip { cursor: pointer; }
.tutor-quick .chip:disabled { opacity: .5; cursor: default; }
.tutor-form { display: flex; gap: 8px; padding: 8px 12px; border-top: 1px solid var(--border); align-items: flex-end; }
.tutor-form[hidden] { display: none; }
.tutor-form textarea { flex: 1; resize: none; min-height: 40px; max-height: 140px; padding: 8px 10px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); background: var(--bg); color: var(--text); font-family: inherit; font-size: 13.5px; }
.tutor-form textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.tutor-foot { padding: 0 12px 10px; color: var(--text-faint); font-size: 11.5px; line-height: 1.45; }
.tutor-foot #tutorRemaining:not(:empty)::after { content: " ·"; }

/* ---------- Responsive ---------- */
.menu-toggle { display: none; }
@media (max-width: 1100px) {
  .tutor { position: fixed; top: 0; right: 0; bottom: 0; width: min(420px, 100vw); z-index: 40; box-shadow: var(--shadow-lg); }
}
@media (max-width: 820px) {
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 30; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow-lg); }
  .sidebar.open { transform: translateX(0); }
  .menu-toggle { display: inline-flex; }
  .prompt-pane { max-height: 50%; }
  .brand small { display: none; }
  .stat.hide-sm, .hide-sm { display: none; }
  .topbar { gap: 8px; padding: 8px 10px; }
  .brief-row, .brief-notes { flex-direction: column; gap: 2px; }
  .brief-k { flex-basis: auto; }
}
