:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --surface-2: #f5f5f5;
  --text: #111111;
  --text-soft: #2a2a2a;
  --muted: #525252;
  --subtle: #737373;
  --hairline: #e5e5e5;
  --hairline-2: #d4d4d4;
  --accent: #0a0a0a;
  --primary: #1d4ed8;
  --primary-soft: #eff6ff;
  --success: #16a34a;
  --success-soft: #ecfdf5;
  --warn: #b45309;
  --warn-soft: #fff7ed;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --kind-strategy: #2563eb;
  --kind-management: #65a30d;
  --kind-technology: #0891b2;
  --safe-bottom: env(safe-area-inset-bottom);
  --safe-top: env(safe-area-inset-top);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Yu Gothic", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  letter-spacing: 0;
  overscroll-behavior-y: contain;
}

body { min-height: 100dvh; }

button, select, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

.tnum { font-variant-numeric: tabular-nums; }

/* =========== Screens =========== */
.screen { min-height: 100dvh; }
.screen-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: var(--bg);
}
.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--hairline);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { color: var(--muted); font-size: 14px; }

/* =========== Login =========== */
.screen-login {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  min-height: 100dvh;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 16px;
  padding: 32px 28px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 1px 2px rgba(0,0,0,.04), 0 8px 24px rgba(0,0,0,.04);
}
.login-title {
  margin: 0 0 8px;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -.02em;
}
.login-sub {
  margin: 0 0 24px;
  color: var(--muted);
  font-size: 14px;
}
#login-form { display: flex; flex-direction: column; gap: 12px; }
#login-password {
  padding: 14px 16px;
  border: 1px solid var(--hairline-2);
  border-radius: 10px;
  font-size: 16px; /* prevent iOS zoom */
  background: var(--surface);
}
#login-password:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
#login-submit {
  padding: 14px 16px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 16px;
}
#login-submit:active { transform: scale(.98); }
.login-error { color: var(--danger); font-size: 13px; margin: 4px 0 0; }

/* =========== Main shell =========== */
.screen-main { display: flex; flex-direction: column; min-height: 100dvh; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  background: rgba(250,250,250,.85);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--hairline);
  padding-top: var(--safe-top);
}
.topbar-inner {
  max-width: 720px;
  margin: 0 auto;
  padding: 12px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand { font-weight: 600; font-size: 15px; }
.sync-state { font-size: 12px; color: var(--muted); }

.container {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px 18px calc(80px + var(--safe-bottom));
  width: 100%;
  flex: 1;
}

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(250,250,250,.92);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--hairline);
  display: flex;
  z-index: 50;
  padding-bottom: var(--safe-bottom);
}
.tab {
  flex: 1;
  appearance: none;
  border: 0;
  background: transparent;
  padding: 9px 4px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
  min-height: 56px;
  transition: color .15s;
}
.tab.active { color: var(--accent); }
.tab-icon { font-size: 22px; line-height: 1; }
.tab-label { font-size: 11px; letter-spacing: -.01em; }

/* =========== Common typography =========== */
.page-title { font-size: 22px; font-weight: 700; letter-spacing: -.02em; margin: 8px 0 16px; }
.page-subtitle { font-size: 14px; font-weight: 600; color: var(--muted); margin: 22px 0 10px; text-transform: none; letter-spacing: 0; }
.section-title { font-size: 14px; font-weight: 600; color: var(--muted); margin: 0 0 10px; }

/* =========== Home =========== */
.home { display: flex; flex-direction: column; gap: 18px; }

.hero-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 14px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.stat-card.primary { background: linear-gradient(135deg, #1e3a8a, #2563eb); color: #fff; border: 0; }
.stat-card.primary .stat-label { color: rgba(255,255,255,.85); }
.stat-card.primary .stat-unit { color: rgba(255,255,255,.7); }
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; }
.stat-unit { font-size: 12px; color: var(--subtle); }

.quick-actions { display: flex; flex-direction: column; gap: 10px; }
.action-card {
  appearance: none;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 14px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.action-card.primary { background: var(--accent); color: #fff; border: 0; }
.action-card.primary .action-sub { color: rgba(255,255,255,.7); }
.action-card:active { transform: scale(.985); }
.action-title { font-size: 15px; font-weight: 600; }
.action-sub { font-size: 12px; color: var(--muted); }

.scorecard {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 14px;
  padding: 16px;
}
.score-grid { display: flex; flex-direction: column; gap: 10px; }
.score-item {
  display: grid;
  grid-template-columns: 100px 1fr 64px;
  align-items: center;
  gap: 8px;
}
.score-item.total { padding-top: 10px; border-top: 1px solid var(--hairline); margin-top: 6px; }
.score-label { font-size: 12px; color: var(--text-soft); }
.score-bar {
  background: var(--hairline);
  border-radius: 999px;
  height: 8px;
  overflow: hidden;
}
.score-bar-fill {
  background: linear-gradient(90deg, var(--primary) 0%, var(--success) 100%);
  height: 100%;
  width: 0;
  transition: width .4s ease;
}
.score-pct { font-size: 12px; color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }

.score-hint { margin: 12px 0 0; font-size: 13px; color: var(--muted); padding: 8px 12px; border-radius: 8px; }
.score-hint[data-level="safe"] { background: var(--success-soft); color: var(--success); }
.score-hint[data-level="caution"] { background: var(--warn-soft); color: var(--warn); }
.score-hint[data-level="danger"] { background: var(--danger-soft); color: var(--danger); }

.weak-topics {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 14px;
  padding: 16px;
}
.weak-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.weak-list li {
  background: var(--surface-2);
  border-radius: 10px;
  padding: 10px 12px;
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 8px;
  font-size: 13px;
  align-items: center;
  cursor: pointer;
}
.weak-list li.empty { background: transparent; color: var(--subtle); cursor: default; }
.weak-rate { color: var(--danger); font-weight: 600; }
.weak-meta { color: var(--subtle); font-size: 11px; }

/* =========== Practice =========== */
.practice { display: flex; flex-direction: column; }
.mode-list { display: grid; grid-template-columns: 1fr; gap: 10px; }
.mode-card {
  appearance: none;
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 14px;
  padding: 14px 16px;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.mode-card:active { transform: scale(.985); }
.mode-title { font-size: 15px; font-weight: 600; }
.mode-sub { font-size: 12px; color: var(--muted); }
.mode-card.domain { border-left: 4px solid var(--accent); }
.mode-card.strategy { border-left-color: var(--kind-strategy); }
.mode-card.management { border-left-color: var(--kind-management); }
.mode-card.technology { border-left-color: var(--kind-technology); }

.section-list { display: grid; grid-template-columns: 1fr; gap: 6px; }
.section-card {
  appearance: none;
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 10px;
  padding: 10px 14px;
  text-align: left;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
}
.section-name { color: var(--text-soft); }
.section-count { color: var(--subtle); font-size: 12px; }

/* =========== Quiz =========== */
.quiz { min-height: 100dvh; display: flex; flex-direction: column; }
.quiz-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--hairline);
  padding: 12px 14px;
  display: grid;
  grid-template-columns: 40px 1fr 60px;
  align-items: center;
  gap: 10px;
  padding-top: calc(12px + var(--safe-top));
}
.quiz-close {
  background: var(--surface-2);
  border: 0;
  border-radius: 8px;
  width: 32px;
  height: 32px;
  font-size: 20px;
  color: var(--muted);
}
.quiz-progress { display: flex; flex-direction: column; gap: 4px; }
.quiz-progress-text { font-size: 12px; color: var(--muted); }
.quiz-progress-bar { height: 4px; background: var(--hairline); border-radius: 2px; overflow: hidden; }
.quiz-progress-fill { height: 100%; background: var(--accent); width: 0; transition: width .3s; }
.quiz-timer { font-variant-numeric: tabular-nums; font-size: 13px; color: var(--text-soft); text-align: right; font-weight: 600; }

.quiz-body {
  flex: 1;
  padding: 16px 18px 120px;
  max-width: 720px;
  margin: 0 auto;
  width: 100%;
}

.qmeta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 14px;
}
.qmeta-domain, .qmeta-section, .qmeta-difficulty {
  background: var(--surface-2);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 11px;
  color: var(--text-soft);
}

.qtext {
  font-size: 16px;
  line-height: 1.65;
  margin-bottom: 16px;
  color: var(--text);
}

.choices { display: flex; flex-direction: column; gap: 8px; }
.choice {
  appearance: none;
  text-align: left;
  background: var(--surface);
  border: 1.5px solid var(--hairline);
  border-radius: 12px;
  padding: 14px 14px;
  display: grid;
  grid-template-columns: 32px 1fr;
  gap: 12px;
  align-items: start;
  min-height: 56px;
  font-size: 14px;
  line-height: 1.55;
  transition: all .15s;
}
.choice:active { transform: scale(.99); }
.choice.selected {
  border-color: var(--primary);
  background: var(--primary-soft);
}
.choice.correct {
  border-color: var(--success);
  background: var(--success-soft);
}
.choice.wrong {
  border-color: var(--danger);
  background: var(--danger-soft);
}
.choice[disabled] { opacity: 1; cursor: default; }
.choice-key {
  background: var(--text);
  color: #fff;
  border-radius: 50%;
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  flex-shrink: 0;
}
.choice.selected .choice-key { background: var(--primary); }
.choice.correct .choice-key { background: var(--success); }
.choice.wrong .choice-key { background: var(--danger); }
.choice-text { word-break: break-word; }

.answer-box {
  margin-top: 20px;
  border-top: 1px solid var(--hairline);
  padding-top: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.result-banner {
  padding: 10px 14px;
  border-radius: 10px;
  font-weight: 700;
  font-size: 16px;
  text-align: center;
}
.result-banner.correct { background: var(--success-soft); color: var(--success); }
.result-banner.wrong { background: var(--danger-soft); color: var(--danger); }
.correct-line { font-size: 13px; color: var(--text-soft); font-weight: 600; }
.explanation h4 { margin: 0 0 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.explanation p {
  margin: 0;
  background: var(--surface-2);
  padding: 14px 16px;
  border-radius: 10px;
  line-height: 1.7;
  font-size: 14px;
  white-space: pre-wrap;
}
.qmeta.sub { margin-bottom: 0; }
.qmeta.sub span { font-size: 11px; color: var(--subtle); }

.quiz-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 12px 16px calc(12px + var(--safe-bottom));
  background: rgba(250,250,250,.92);
  backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--hairline);
  display: flex;
  gap: 8px;
  z-index: 20;
}

.btn {
  appearance: none;
  border: 0;
  border-radius: 10px;
  padding: 14px 16px;
  font-size: 15px;
  font-weight: 600;
  flex: 1;
  min-height: 48px;
}
.btn:active { transform: scale(.985); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:disabled { background: var(--hairline-2); color: var(--muted); }
.btn-secondary { background: var(--surface); color: var(--text); border: 1px solid var(--hairline-2); }
.btn-danger { background: var(--danger); color: #fff; }

/* =========== Result =========== */
.result { display: flex; flex-direction: column; gap: 18px; }
.result-summary {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 14px;
  padding: 24px;
  text-align: center;
}
.result-pct { display: block; font-size: 48px; font-weight: 800; letter-spacing: -.04em; line-height: 1; }
.result-sub { display: block; margin-top: 6px; color: var(--muted); font-size: 14px; }
.result-grid { display: flex; flex-direction: column; gap: 10px; background: var(--surface); border: 1px solid var(--hairline); border-radius: 14px; padding: 16px; }
.result-wrong { background: var(--surface); border: 1px solid var(--hairline); border-radius: 14px; padding: 16px; }
.wrong-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.wrong-list li { padding: 10px 12px; background: var(--surface-2); border-radius: 10px; display: flex; flex-direction: column; gap: 3px; }
.wrong-list li.empty { background: transparent; color: var(--success); font-weight: 600; }
.wrong-q { font-size: 13px; color: var(--text-soft); line-height: 1.5; }
.wrong-meta { font-size: 11px; color: var(--subtle); }
.result-actions { display: flex; gap: 10px; }

/* =========== Stats =========== */
.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat-block {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 14px;
  padding: 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
}
.stat-block .stat-value { font-size: 22px; }

.recent-bars {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 14px;
  padding: 16px;
}
.recent-bar { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.recent-bar-track {
  width: 24px;
  height: 80px;
  background: var(--hairline);
  border-radius: 6px;
  position: relative;
  overflow: hidden;
}
.recent-bar-fill {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(180deg, var(--primary), var(--success));
}
.recent-bar-count { font-size: 11px; color: var(--text-soft); font-variant-numeric: tabular-nums; }
.recent-bar-date { font-size: 10px; color: var(--subtle); }

/* =========== Settings =========== */
.settings { display: flex; flex-direction: column; gap: 22px; }
.settings-section {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 14px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.meta-text { white-space: pre-wrap; font-size: 12px; color: var(--muted); font-family: ui-monospace, SFMono-Regular, monospace; margin: 0; }

/* =========== Modal / Toast =========== */
.modal-back {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}
.modal-box {
  background: var(--surface);
  border-radius: 16px;
  padding: 24px;
  max-width: 360px;
  width: 100%;
}
.modal-box h3 { margin: 0 0 12px; font-size: 17px; }
.modal-box ol { margin: 0 0 16px 22px; padding: 0; font-size: 14px; line-height: 1.7; }
.modal-close {
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 12px;
  font-weight: 600;
}
.icon-share { background: var(--surface-2); border-radius: 4px; padding: 0 4px; font-size: 14px; }

.toast {
  position: fixed;
  bottom: calc(80px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #1a1a1a;
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 200;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* =========== Dark mode =========== */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0a0a0a;
    --surface: #141414;
    --surface-2: #1a1a1a;
    --text: #f5f5f5;
    --text-soft: #d4d4d4;
    --muted: #a3a3a3;
    --subtle: #737373;
    --hairline: #2a2a2a;
    --hairline-2: #3d3d3d;
    --accent: #f5f5f5;
    --primary: #60a5fa;
    --primary-soft: rgba(96,165,250,.12);
    --success: #4ade80;
    --success-soft: rgba(74,222,128,.12);
    --warn: #fbbf24;
    --warn-soft: rgba(251,191,36,.12);
    --danger: #f87171;
    --danger-soft: rgba(248,113,113,.12);
  }
  .topbar { background: rgba(10,10,10,.85); }
  .tabbar { background: rgba(10,10,10,.92); }
  .stat-card.primary { background: linear-gradient(135deg, #1e3a8a, #1d4ed8); }
  #login-submit, .action-card.primary, .btn-primary, .modal-close, .tab.active { color: #0a0a0a; }
  .action-card.primary .action-sub { color: rgba(10,10,10,.6); }
  .stat-card.primary .stat-label { color: rgba(255,255,255,.85); }
  .stat-card.primary .stat-unit { color: rgba(255,255,255,.7); }
}
