/* App chrome: shell, collapsible sidebar, top bar, sync chip, tags, buttons, dialog, footer. */

/* ---------- shell ---------- */
.shell { min-height: 100vh; display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); }
.content { min-width: 0; display: flex; flex-direction: column; min-height: 100vh; }
.app { flex: 1; outline: none; container-type: inline-size; }
.scrim { display: none; }

/* ---------- sidebar ---------- */
.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 10px 8px 24px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--line);
  font-size: 0.875rem;
}
.sb-brand { display: flex; align-items: center; gap: 8px; padding: 6px 8px 10px; font-weight: 600; text-decoration: none; }
.sb-logo {
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  background: var(--ink);
  color: var(--bg);
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 700;
}
.sb-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 28px;
  padding: 3px 8px;
  border-radius: var(--radius);
  color: var(--muted);
  text-decoration: none;
  transition: background-color 0.12s ease, color 0.12s ease;
}
/* Each row carries a progress fill: --p (0-100) is set by paintProgress. Only background-COLOR changes on
   hover and for the current page, so the fill (a background-image) stays visible underneath. */
.sb-link {
  --fill: var(--accent-soft);
  background-image: linear-gradient(90deg, var(--fill) calc(var(--p, 0) * 1%), transparent calc(var(--p, 0) * 1%));
  background-repeat: no-repeat;
}
.sb-link:hover { background-color: var(--bg-hover); color: var(--ink); }
.sb-link[aria-current="page"] { background-color: var(--bg-active); color: var(--ink); font-weight: 600; }
.sb-cnt { font-size: 0.75rem; font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--faint); }
.sb-link[aria-current="page"] .sb-cnt { color: var(--muted); }
.sb-label { margin: 16px 8px 4px; font-size: 0.75rem; font-weight: 600; color: var(--faint); }

/* A row's left group: icon + label, so justify-content: space-between still puts the count on the right. */
.sb-left { display: flex; align-items: center; gap: 8px; min-width: 0; }
.sb-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sb-link .ico {
  flex: 0 0 auto;
  width: 18px;
  font-size: 0.95rem;
  line-height: 1;
  text-align: center;
  transition: transform 0.12s ease;
}
.sb-link:hover .ico { transform: scale(1.12); }

/* These come AFTER the base .sidebar rule on purpose: same specificity, so the later one wins. */

/* Desktop: the sidebar can be folded away. main.js sets body.sidebar-collapsed and remembers it. */
@media (min-width: 900px) {
  body.sidebar-collapsed .shell { grid-template-columns: minmax(0, 1fr); }
  body.sidebar-collapsed .sidebar { display: none; }
}

/* Phone and tablet: the sidebar is a drawer over the page. */
@media (max-width: 899px) {
  .shell { display: block; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 50;
    width: min(84vw, 300px);
    transform: translateX(-104%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow-pop);
  }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .scrim { display: block; position: fixed; inset: 0; z-index: 40; background: rgba(15, 15, 15, 0.4); }
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  height: calc(var(--topbar-h) + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) 12px 0;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.icon-btn {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  padding: 0;
  background: none;
  border: 0;
  border-radius: var(--radius);
  color: var(--muted);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--ink); }
.icon-btn svg { width: 18px; height: 18px; }

.crumb { min-width: 0; flex: 1; display: flex; align-items: center; gap: 6px; overflow: hidden; white-space: nowrap; font-size: 0.875rem; color: var(--muted); }
.crumb a { text-decoration: none; }
.crumb a:hover { color: var(--ink); text-decoration: underline; }
.crumb .here { overflow: hidden; text-overflow: ellipsis; color: var(--ink); }

.overall { display: flex; align-items: center; gap: 10px; }
.overall-text { font-size: 0.8125rem; font-variant-numeric: tabular-nums; color: var(--muted); white-space: nowrap; }
.overall .track { width: 72px; }
@media (max-width: 560px) { .overall .track { display: none; } }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 4px 10px;
  background: none;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  font-size: 0.8125rem;
  white-space: nowrap;
}
.chip:hover { background: var(--bg-hover); }
.chip-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); }
.chip[data-state="syncing"] .chip-dot,
.chip[data-state="offline"] .chip-dot { background: #dfab01; }
.chip[data-state="synced"] .chip-dot { background: var(--ok); }
.chip[data-state="rejected"] .chip-dot,
.chip[data-state="error"] .chip-dot { background: var(--bad); }

/* ---------- small shared pieces ---------- */
.track { height: 4px; background: var(--line-strong); border-radius: 2px; overflow: hidden; }
.track i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 2px; transition: width 0.25s ease; }

.tag {
  display: inline-block;
  padding: 0 6px;
  border-radius: 3px;
  font-size: 0.75rem;
  line-height: 20px;
  white-space: nowrap;
  background: var(--tag-gray-bg);
  color: var(--tag-gray-fg);
}
.tag-easy { background: var(--tag-green-bg); color: var(--tag-green-fg); }
.tag-medium { background: var(--tag-yellow-bg); color: var(--tag-yellow-fg); }
.tag-hard { background: var(--tag-red-bg); color: var(--tag-red-fg); }
.tag-group { background: var(--gt-bg); color: var(--gt-fg); }

.btn {
  padding: 5px 12px;
  background: var(--bg);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  font-size: 0.875rem;
  text-decoration: none;
}
.btn:hover { background: var(--bg-hover); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-quiet { border-color: transparent; color: var(--muted); }

/* ---------- sync dialog ---------- */
.sync-dialog {
  width: min(34rem, calc(100% - 32px));
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  border: 0;
  border-radius: 6px;
  box-shadow: var(--shadow-pop);
}
.sync-dialog::backdrop { background: rgba(15, 15, 15, 0.5); }
.sync-form { display: grid; gap: 14px; padding: 22px; }
.sync-form h2 { font-size: 1.25rem; }
.sync-form p { color: var(--muted); font-size: 0.9375rem; }
.sync-steps { margin: 0; padding-left: 20px; display: grid; gap: 6px; font-size: 0.9375rem; }
.sync-steps a { color: var(--accent); }
.sync-field { font-size: 0.8125rem; font-weight: 600; color: var(--muted); }
#sync-token {
  width: 100%;
  padding: 7px 10px;
  background: var(--bg-soft);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  font: 0.875rem var(--font-mono);
}
#sync-token:focus { outline: 0; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.sync-status { min-height: 1.5em; font-size: 0.875rem; }
.sync-status[data-tone="ok"] { color: var(--ok); }
.sync-status[data-tone="bad"] { color: var(--bad); }
.sync-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.sync-caveats { font-size: 0.875rem; color: var(--muted); }
.sync-caveats summary { cursor: pointer; }
.sync-caveats ul { margin: 8px 0 0; padding-left: 20px; display: grid; gap: 6px; }

/* ---------- footer ---------- */
.site-footer { padding: 24px clamp(16px, 5vw, 48px) 40px; color: var(--muted); font-size: 0.8125rem; }
.site-footer a { margin-left: 8px; }

/* ---------- search button, thin progress line, key caps ---------- */
kbd {
  padding: 1px 5px;
  background: var(--bg);
  border: 1px solid var(--line-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  color: var(--muted);
  font: 500 0.6875rem var(--font-mono);
}
.search-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px 4px 10px;
  background: none;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  color: var(--muted);
  font-size: 0.8125rem;
}
.search-btn:hover { background: var(--bg-hover); color: var(--ink); }
.search-btn svg { width: 15px; height: 15px; }
@media (max-width: 640px) {
  .search-label, .search-btn kbd { display: none; }
  .search-btn { padding: 4px 7px; }
}

.topline { display: none; position: absolute; left: 0; right: 0; bottom: -1px; height: 2px; }
.topline i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.25s ease; }
@media (max-width: 560px) { .topline { display: block; } }

/* ---------- command palette: a Spotlight-style panel over a dimmed, blurred page ---------- */
.palette, .help {
  color: var(--ink);
  border: 0;
  padding: 0;
}
.palette::backdrop, .help::backdrop {
  background: rgba(20, 20, 20, 0.3);
  -webkit-backdrop-filter: blur(14px) saturate(1.1);
  backdrop-filter: blur(14px) saturate(1.1);
}
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .palette::backdrop, .help::backdrop { background: rgba(20, 20, 20, 0.6); }
}

.palette {
  position: fixed;
  top: 14vh;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  width: min(640px, calc(100vw - 32px));
  max-width: none;
  background: transparent;
  overflow: visible;
}
.pal-box {
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  -webkit-backdrop-filter: blur(28px) saturate(1.3);
  backdrop-filter: blur(28px) saturate(1.3);
  border: 1px solid var(--line-strong);
  border-radius: 14px;
  box-shadow: var(--shadow-pop);
  overflow: hidden;
}
#pal-input {
  width: 100%;
  padding: 16px 20px;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--line);
  font-size: 1.125rem;
  outline: none;
}
#pal-input::placeholder { color: var(--faint); }
.pal-list { list-style: none; margin: 0; padding: 6px; max-height: min(52vh, 420px); overflow-y: auto; }
.pal-list [role="option"] {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
}
.pal-list [aria-selected="true"] { background: var(--accent); color: #fff; }
.pal-main { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.pal-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.pal-sub { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 0.8125rem; }
.pal-meta { display: flex; align-items: center; gap: 8px; flex: none; color: var(--muted); font-size: 0.75rem; }
.pal-list [aria-selected="true"] .pal-sub,
.pal-list [aria-selected="true"] .pal-meta { color: rgba(255, 255, 255, 0.78); }
.pal-diff::before { content: ""; display: inline-block; width: 7px; height: 7px; margin-right: 5px; border-radius: 50%; background: var(--c-med); }
.pal-diff-e::before { background: var(--c-easy); }
.pal-diff-h::before { background: var(--c-hard); }
.pal-done { color: var(--ok); font-weight: 700; }
.pal-star { color: #dfab01; }
.pal-list [aria-selected="true"] .pal-done, .pal-list [aria-selected="true"] .pal-star { color: #fff; }
.pal-empty { padding: 18px 14px; color: var(--muted); font-size: 0.9rem; }
.pal-foot { display: flex; flex-wrap: wrap; gap: 6px 16px; padding: 8px 16px; border-top: 1px solid var(--line); color: var(--muted); font-size: 0.75rem; }
.pal-foot span { display: inline-flex; align-items: center; gap: 4px; }
@media (max-width: 560px) { .palette { top: 8vh; } .pal-foot { display: none; } }
@media (prefers-reduced-motion: no-preference) {
  .palette[open] { animation: pal-in 0.12s ease-out; }
  .palette[open]::backdrop, .help[open]::backdrop { animation: fade-in 0.12s ease-out; }
}
@keyframes pal-in { from { opacity: 0; transform: translateX(-50%) scale(0.98); } }
@keyframes fade-in { from { opacity: 0; } }

/* ---------- keyboard shortcut sheet ---------- */
.help { width: min(680px, calc(100% - 32px)); background: var(--bg); border-radius: 14px; box-shadow: var(--shadow-pop); }
.help-box { padding: 20px 24px 24px; }
.help-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.help-head h2 { font-size: 1.25rem; }
.help-body { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 20px 32px; }
.help-body h3 { margin-bottom: 8px; color: var(--muted); font-size: 0.8125rem; }
.help-body dl { display: grid; gap: 6px; }
.help-body dl div { display: grid; grid-template-columns: 96px minmax(0, 1fr); align-items: center; gap: 8px; font-size: 0.875rem; }
.help-body dt { display: flex; flex-wrap: wrap; gap: 4px; }
.help-body dd { margin: 0; color: var(--muted); }

/* ---------- study assistant panel ---------- */
.ai-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
  border-left: 1px solid var(--line);
}
.ai-head { display: flex; align-items: center; gap: 10px; padding: 10px 12px 8px 16px; }
.ai-head h2 { font-size: 1rem; letter-spacing: 0; }
.ai-engine-chip { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 0.75rem; }
.ai-head-actions { display: flex; gap: 2px; }
.ai-context { display: flex; align-items: center; gap: 8px; padding: 0 16px 8px; }
.ai-label { color: var(--muted); font-size: 0.8125rem; }
.ai-context select { flex: 1; min-width: 0; padding: 4px 8px; background: var(--bg); border: 1px solid var(--line-strong); border-radius: var(--radius); font-size: 0.8125rem; }
.ai-modes { margin: 0 16px 8px; display: flex; }
.ai-modes button { flex: 1; padding-inline: 4px; }
.seg button:disabled { opacity: 0.5; cursor: default; }

.ai-setup { margin: 0 16px 8px; padding: 12px 14px; background: var(--bg-soft); border-radius: var(--radius); font-size: 0.875rem; }
.ai-setup p { margin-bottom: 10px; color: var(--muted); }

.ai-log { flex: 1; min-height: 0; overflow-y: auto; display: grid; align-content: start; gap: 14px; padding: 8px 16px 14px; border-top: 1px solid var(--line); }
.ai-empty { padding: 18px 2px; color: var(--muted); font-size: 0.9375rem; }
.ai-who { margin-bottom: 4px; color: var(--muted); font-size: 0.75rem; }
.ai-msg.user .ai-body { padding: 8px 12px; background: var(--bg-soft); border-radius: 8px; white-space: pre-wrap; font-size: 0.9375rem; }
.ai-msg.assistant .ai-body { font-size: 0.9375rem; line-height: 1.55; overflow-wrap: anywhere; }
.ai-body p { margin: 0 0 0.65em; }
.ai-body p:last-child { margin-bottom: 0; }
.ai-body h4 { margin: 0.9em 0 0.3em; font-size: 0.9375rem; font-weight: 600; }
.ai-body ul, .ai-body ol { margin: 0 0 0.65em; padding-left: 1.3em; }
.ai-body li + li { margin-top: 0.25em; }
.ai-body .code { margin: 0.5em 0 0.75em; }
.ai-body .code pre { font-size: 0.78rem; }
.ai-code { margin-top: 6px; font-size: 0.8125rem; color: var(--muted); }
.ai-code summary { cursor: pointer; }
.ai-code pre { margin: 6px 0 0; padding: 10px 12px; max-height: 220px; overflow: auto; background: var(--bg-soft); border-radius: var(--radius); font: 0.78rem / 1.55 var(--font-mono); }
.ai-code pre code { padding: 0; background: none; color: var(--ink); }
.ai-chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.ai-err { padding: 10px 12px; background: var(--tag-red-bg); color: var(--tag-red-fg); border-radius: var(--radius); font-size: 0.875rem; }
.ai-err p { margin-bottom: 8px; }
.ai-err p:last-child { margin-bottom: 0; }
.ai-problem { padding: 0 6px; background: var(--tag-blue-bg); border: 0; border-radius: 3px; color: var(--tag-blue-fg); font: inherit; font-size: 0.875rem; line-height: 20px; }
.ai-problem:hover { text-decoration: underline; }
.ai-cursor { display: inline-block; width: 7px; height: 1em; margin-left: 2px; vertical-align: text-bottom; background: var(--muted); }
@media (prefers-reduced-motion: no-preference) { .ai-cursor { animation: ai-blink 1s steps(2, start) infinite; } }
@keyframes ai-blink { to { visibility: hidden; } }

.ai-form { display: grid; gap: 8px; padding: 10px 16px 14px; border-top: 1px solid var(--line); background: var(--bg); }
.ai-row { display: grid; gap: 8px; }
.ai-row[hidden] { display: none; }
.ai-form textarea, .ai-form input[type="text"] { width: 100%; padding: 7px 10px; background: var(--bg); border: 1px solid var(--line-strong); border-radius: var(--radius); font: inherit; font-size: 0.875rem; resize: vertical; }
.ai-form textarea:focus, .ai-form input:focus, .ai-context select:focus, .ai-model-row input:focus { outline: 0; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
#ai-code { font-family: var(--font-mono); font-size: 0.78rem; line-height: 1.5; tab-size: 2; white-space: pre; overflow: auto; }
.ai-two { display: grid; gap: 8px; }
.ai-actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; }
.ai-note { flex: 1; min-width: 0; color: var(--muted); font-size: 0.75rem; }
.ai-note[data-tone="bad"] { color: var(--bad); }
.btn-danger { background: var(--tag-red-bg); border-color: transparent; color: var(--tag-red-fg); }
.btn-danger:hover { background: var(--tag-red-bg); filter: brightness(0.96); }
.btn:disabled { opacity: 0.6; cursor: default; }

/* Settings dialog extras (the dialog itself reuses .sync-dialog and .sync-form). */
.ai-engines { display: grid; gap: 8px; }
.ai-engine { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border: 1px solid var(--line-strong); border-radius: var(--radius); cursor: pointer; }
.ai-engine:hover { background: var(--bg-hover); }
.ai-engine:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.ai-engine input { accent-color: var(--accent); }
.ai-engine-name { font-weight: 500; }
.ai-blurb { margin: 0; color: var(--muted); font-size: 0.875rem; }
#ai-key, .ai-model-row input { width: 100%; padding: 7px 10px; background: var(--bg-soft); border: 1px solid var(--line-strong); border-radius: var(--radius); font: 0.875rem var(--font-mono); }
#ai-key:focus { outline: 0; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.ai-model-row { display: flex; gap: 8px; }
.ai-model-row input { flex: 1; min-width: 0; }
.sync-field a { color: var(--accent); font-weight: 400; }

/* Layout. These come after the base rules on purpose (same specificity: the later one wins). */
@media (min-width: 1100px) {
  body.ai-open .shell { grid-template-columns: var(--sidebar-w) minmax(0, 1fr) 400px; }
  body.ai-open.sidebar-collapsed .shell { grid-template-columns: minmax(0, 1fr) 400px; }
  .ai-panel { position: sticky; top: 0; height: 100vh; height: 100dvh; }
}
@media (max-width: 1099px) {
  .ai-panel { position: fixed; inset: 0 0 0 auto; z-index: 60; width: min(100vw, 440px); height: 100dvh; box-shadow: var(--shadow-pop); }
}
@media (max-width: 560px) { .ai-two { grid-template-columns: 1fr; } }
@media (min-width: 561px) { .ai-two { grid-template-columns: 1fr 1fr; } }
