/* Shared styles for all pages. Colors are tokens so dark mode only swaps values. */

:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f1efe9;
  --border: #e4e1d8;
  --text: #1d1f23;
  --text-muted: #6b6f76;
  --accent: #3b5bdb;
  --accent-hover: #2f4ac0;
  --accent-soft: #e8edff;
  --accent-text: #ffffff;
  --danger: #c92a2a;
  --danger-soft: #ffeaea;
  --success: #2b8a3e;
  --chart-bar: #3b5bdb;        /* validated: lightness band, chroma, >= 3:1 on the surface */
  --shadow: 0 1px 2px rgba(20, 20, 30, 0.04), 0 4px 16px rgba(20, 20, 30, 0.05);
  --radius: 14px;
  --radius-sm: 9px;
  --topbar-h: 60px;
  --font: "Inter", "Noto Sans JP", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SF Mono", Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121316;
    --surface: #1b1c20;
    --surface-2: #24262b;
    --border: #2f3137;
    --text: #e8e8eb;
    --text-muted: #9b9da4;
    --accent: #7d93ff;
    --accent-hover: #97a8ff;
    --accent-soft: #242b47;
    --accent-text: #0f1220;
    --danger: #ff6b6b;
    --danger-soft: #3a2023;
    --success: #69db7c;
    --chart-bar: #6f86f5;      /* dark step validated against the dark surface */
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 16px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }

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

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

h1, h2, h3 { line-height: 1.25; margin: 0; }

.muted { color: var(--text-muted); }
.hidden { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0 max(16px, calc((100vw - 1160px) / 2));
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 650;
  color: var(--text);
  letter-spacing: -0.01em;
}
.brand:hover { color: var(--text); }
.brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-text);
  font-family: "Noto Sans JP", var(--font);
  font-weight: 700;
  font-size: 1rem;
}
.topbar-spacer { flex: 1; }
.badge {
  display: inline-block;
  min-width: 1.4em;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.75rem;
  font-weight: 700;
  line-height: 1.4em;
  text-align: center;
}
/* phones: the logo alone, so the top bar links fit on one line */
@media (max-width: 600px) {
  .brand-name { display: none; }
}

/* ---------- Layout ---------- */
.container {
  width: 100%;
  max-width: 1160px;
  margin: 0 auto;
  padding: 28px 16px 64px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
}
@media (max-width: 600px) {
  .card { padding: 18px; }
}

.section-title {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin: 0 0 12px;
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 0.6rem 1.05rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.92rem;
  font-weight: 550;
  line-height: 1.2;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.05s;
}
.btn:hover { background: var(--surface-2); color: var(--text); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn:focus-visible, .input:focus-visible, .icon-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: var(--accent-text); }

.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--surface-2); }

.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); color: var(--danger); border-color: var(--danger-soft); }

.btn-sm { padding: 0.4rem 0.75rem; font-size: 0.85rem; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.1rem;
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }

/* ---------- Inputs ---------- */
.input {
  width: 100%;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea.input { resize: vertical; line-height: 1.6; }
.input::placeholder { color: var(--text-muted); opacity: 0.8; }

.field-label {
  display: block;
  font-size: 0.88rem;
  font-weight: 550;
  margin-bottom: 6px;
}

.alert {
  padding: 0.75rem 1rem;
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 0.92rem;
}

/* ---------- Live progress panel ---------- */
.progress-panel {
  margin-bottom: 16px;
  padding: 14px 18px;
}
.progress-status {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 600;
  font-size: 0.95rem;
}
.progress-status .dot {
  flex: none;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.2s ease-in-out infinite;
}
.progress-panel.finished .dot { animation: none; background: var(--success); }
.progress-panel.failed .dot { animation: none; background: var(--danger); }
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(0.8); }
}
.progress-elapsed {
  margin-left: auto;
  font-weight: 400;
  font-size: 0.85em;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}
.progress-log {
  margin-top: 10px;
  height: 8.5rem;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 0.78rem;
  line-height: 1.5;
  color: var(--text-muted);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  /* fade out older lines at the top */
  mask-image: linear-gradient(to bottom, transparent, black 2.5rem);
  -webkit-mask-image: linear-gradient(to bottom, transparent, black 2.5rem);
}
.progress-log .log-step { color: var(--accent); font-weight: 600; }
.progress-log .log-thinking { font-style: italic; }

/* ---------- Rendered markdown (overrides github-markdown-css) ---------- */
.markdown-body {
  background: transparent !important;
  color: var(--text) !important;
  font-family: var(--font) !important;
  font-size: 1rem;
  line-height: 1.75;
}
.markdown-body h1, .markdown-body h2 {
  border-bottom-color: var(--border) !important;
}
.markdown-body h1 { font-size: 1.6em; }
.markdown-body a { color: var(--accent); }
.markdown-body table { font-size: 0.92em; }
.markdown-body table tr { background: transparent !important; border-color: var(--border) !important; }
.markdown-body table tr:nth-child(2n) { background: var(--surface-2) !important; }
.markdown-body table th, .markdown-body table td { border-color: var(--border) !important; }
.markdown-body code { background: var(--surface-2) !important; }
.markdown-body blockquote { color: var(--text-muted) !important; border-left-color: var(--border) !important; }
.markdown-body hr { background-color: var(--border) !important; }
@media (max-width: 768px) and (pointer: coarse) {
  .markdown-body { font-size: 1.05rem; }
}

/* ---------- Home page ---------- */
.home-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 24px;
  align-items: start;
}
@media (max-width: 900px) {
  .home-grid { grid-template-columns: minmax(0, 1fr); }
}

.hero h1 { font-size: 1.6rem; letter-spacing: -0.02em; }
.hero p { margin: 6px 0 20px; color: var(--text-muted); }

.composer textarea { min-height: 240px; font-size: 1rem; }.composer-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 12px;
  flex-wrap: wrap;
}
.kbd-hint { font-size: 0.82rem; color: var(--text-muted); }
kbd {
  font-family: var(--mono);
  font-size: 0.78rem;
  padding: 1px 6px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface-2);
}

.history-card { position: sticky; top: calc(var(--topbar-h) + 20px); padding: 18px; }
.history-search { margin-bottom: 10px; padding: 0.5rem 0.75rem; font-size: 0.9rem; }
.history-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-height: calc(100vh - var(--topbar-h) - 170px);
  overflow-y: auto;
  margin: 0 -6px;
}
@media (max-width: 900px) {
  .history-card { position: static; }
  .history-list { max-height: none; }
}
.history-item {
  display: flex;
  border-radius: var(--radius-sm);
}
.history-item:hover { background: var(--surface-2); }
.history-item a {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  color: var(--text);
}
.history-title {
  display: block;
  font-size: 0.92rem;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.history-meta { display: block; font-size: 0.78rem; color: var(--text-muted); }
.empty-state {
  padding: 24px 8px;
  text-align: center;
  font-size: 0.9rem;
  color: var(--text-muted);
}

/* ---------- Text page ---------- */
.doc-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 24px;
  align-items: start;
}
.doc-layout.chat-open { grid-template-columns: minmax(0, 1fr) 400px; }

.doc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.back-link { font-size: 0.9rem; color: var(--text-muted); }
.back-link:hover { color: var(--text); }
.doc-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.doc-card { padding: 32px 40px; }
@media (max-width: 600px) {
  .doc-card { padding: 20px 18px; }
}

/* ---------- Chat panel ---------- */
.chat-panel {
  position: sticky;
  top: calc(var(--topbar-h) + 20px);
  height: calc(100vh - var(--topbar-h) - 48px);
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
}
@media (max-width: 1000px) {
  /* full-screen sheet on small screens */
  .doc-layout.chat-open { grid-template-columns: minmax(0, 1fr); }
  .chat-panel {
    position: fixed;
    inset: var(--topbar-h) 0 0 0;
    top: var(--topbar-h);
    height: auto;
    z-index: 60;
    border-radius: 0;
    border-left: none;
    border-right: none;
  }
}
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 12px 12px 18px;
  border-bottom: 1px solid var(--border);
}
.chat-header h2 { font-size: 1rem; }
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.chat-empty {
  margin: auto 0;
  text-align: center;
  font-size: 0.9rem;
  color: var(--text-muted);
  padding: 0 8px;
}
.chat-empty strong { color: var(--text); }
.msg {
  max-width: 92%;
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 0.93rem;
  line-height: 1.6;
  overflow-wrap: anywhere;
}
.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-text);
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}
.msg.assistant {
  align-self: flex-start;
  background: var(--surface-2);
  border-bottom-left-radius: 4px;
}
.msg.assistant.markdown-body {
  background: var(--surface-2) !important;   /* beat the transparent markdown override */
  font-size: 0.93rem;
  line-height: 1.6;
}
.msg.assistant.markdown-body > :first-child { margin-top: 0; }
.msg.assistant.markdown-body > :last-child { margin-bottom: 0; }
.msg.note {
  align-self: center;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.82rem;
  padding: 2px 8px;
}
.msg.error { background: var(--danger-soft); color: var(--danger); }
.msg-tag {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  opacity: 0.8;
  margin-bottom: 2px;
}
.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: pulse 1s ease-in-out infinite;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

.chat-composer {
  border-top: 1px solid var(--border);
  padding: 12px;
}
.chat-composer textarea { min-height: 72px; max-height: 200px; font-size: 0.93rem; }
.chat-composer-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}
.chat-composer-actions .btn { flex: 1; }

/* ---------- Login ---------- */
.auth-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.auth-card { width: 100%; max-width: 380px; padding: 32px; }
.auth-card .brand { justify-content: center; margin-bottom: 20px; font-size: 1.1rem; }
.auth-card h1 { font-size: 1.35rem; text-align: center; }
.auth-card p.muted { text-align: center; margin: 4px 0 22px; font-size: 0.92rem; }
.auth-card form { display: flex; flex-direction: column; gap: 14px; }
.auth-card .btn { width: 100%; padding: 0.75rem; }
.auth-msg { min-height: 1.4em; margin-top: 12px; font-size: 0.88rem; text-align: center; }
.auth-msg.error { color: var(--danger); }
.auth-msg.success { color: var(--success); }

/* ---------- Shared controls (segmented switch, filter chips, JLPT badges) ---------- */
.segmented {
  display: inline-flex;
  padding: 3px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.segmented a, .segmented button {
  padding: 0.45rem 0.95rem;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 550;
  cursor: pointer;
}
.segmented a:hover, .segmented button:hover { color: var(--text); }
.segmented .active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

.chip {
  padding: 0.3rem 0.7rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font: inherit;
  font-size: 0.82rem;
  font-weight: 550;
  cursor: pointer;
}
.chip .count { opacity: 0.65; font-weight: 400; margin-left: 6px; }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.chip.link { border-color: transparent; background: transparent; }
.chip.link:hover { color: var(--text); }

/* JLPT level badges: N5 (easy, green) -> N1 (hard, red) */
.jlpt {
  display: inline-block;
  min-width: 2.4em;
  padding: 1px 7px;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 700;
  text-align: center;
  white-space: nowrap;
}
.jlpt-N5 { background: rgba(43, 138, 62, 0.15); color: #2b8a3e; }
.jlpt-N4 { background: rgba(102, 168, 15, 0.16); color: #5c940d; }
.jlpt-N3 { background: rgba(240, 140, 0, 0.16); color: #d9480f; }
.jlpt-N2 { background: rgba(232, 89, 12, 0.16); color: #e8590c; }
.jlpt-N1 { background: rgba(201, 42, 42, 0.15); color: #c92a2a; }
.jlpt-unknown { background: var(--surface-2); color: var(--text-muted); }
@media (prefers-color-scheme: dark) {
  .jlpt-N5 { color: #69db7c; }
  .jlpt-N4 { color: #a9e34b; }
  .jlpt-N3 { color: #ffc078; }
  .jlpt-N2 { color: #ffa94d; }
  .jlpt-N1 { color: #ff8787; }
}

/* ---------- Quiz card (study-page quizzes and the flashcard quiz share this) ---------- */
.quiz-container {
  position: relative;
  max-width: 540px;
  margin: 0 auto;
  text-align: center;
  padding: 28px;
}

.quiz-container h1, .quiz-container h2 { margin: 0.4rem 0 1rem; }
.quiz-container h2 { font-size: 1.3rem; }
.quiz-container h2.question {
  font-family: "Noto Sans JP", var(--font);
  font-size: 2rem;
  font-weight: 500;
  margin: 1.6rem 0 1.4rem;
  overflow-wrap: anywhere;
}

.quiz-container .progress {
  position: absolute;
  top: 14px;
  right: 16px;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  user-select: none;
}
/* All quiz buttons look like .btn, full width */
.quiz-container button, .modal-content button {
  display: block;
  width: 100%;
  margin-top: 0.7rem;
  padding: 0.8rem 1rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.quiz-container button:hover, .modal-content button:hover { background: var(--surface-2); }
.quiz-container button:active, .modal-content button:active { transform: scale(0.99); }

/* Answer options */
.quiz-container #answers button {
  font-family: "Noto Sans JP", var(--font);
  font-size: 1.05rem;
  padding: 0.95rem 1rem;
}
.quiz-container #answers button:hover { border-color: var(--accent); background: var(--accent-soft); }

.quiz-container #startBtn, .quiz-container #continueBtn, .close-modal-btn {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
.quiz-container #startBtn:hover, .quiz-container #continueBtn:hover, .close-modal-btn:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
.incorrect-slide {
  background: var(--danger-soft);
  border-radius: var(--radius-sm);
  padding: 1rem 1.25rem;
  color: var(--danger);
  margin-top: 1.5rem;
  text-align: left;
}
.incorrect-slide h2 { margin-top: 0; }
.incorrect-slide p { margin: 0.4rem 0; color: var(--text); }

.correct-slide {
  background: color-mix(in srgb, var(--success) 14%, transparent);
  border-radius: var(--radius-sm);
  padding: 1rem 1.25rem;
  color: var(--success);
  margin-top: 1.5rem;
}
.correct-slide h2 { margin: 0 0 0.3rem; }
.correct-slide p { margin: 0; color: var(--text); font-size: 1.05rem; }

/* Kanji quiz: the word(s) a kanji comes from, with furigana */
.source-box {
  margin-top: 12px;
  padding: 0.9rem 1.1rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  text-align: left;
}
.source-box.empty { color: var(--text-muted); font-size: 0.88rem; text-align: center; }
.source-label {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.source-word { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; padding: 4px 0; }
.source-jp { font-size: 1.7rem; line-height: 1.9; }
.source-jp rt { font-size: 0.45em; color: var(--text-muted); }
.source-jp mark { background: none; color: var(--accent); font-weight: 700; }
.source-meaning { color: var(--text-muted); }

.tap-hint { margin: 14px 0 0; font-size: 0.82rem; color: var(--text-muted); }
.quiz-keys { margin: 12px 0 0; font-size: 0.78rem; color: var(--text-muted); }
/* furigana on quiz questions */
.quiz-container h2.question:has(ruby) { line-height: 2.1; }
.quiz-container h2.question rt { font-size: 0.42em; font-weight: 400; color: var(--text-muted); }
.reading-below { display: block; margin-top: 6px; font-size: 0.55em; font-weight: 400; color: var(--text-muted); }
/* no keyboard on phones/tablets */
@media (hover: none), (pointer: coarse), (max-width: 700px) { .quiz-keys { display: none; } }
@media (max-width: 480px) {
  .quiz-container { padding: 20px 16px; }
  .quiz-container h2.question { font-size: 1.6rem; }
}
