:root {
  color-scheme: light dark;
  --bg: #f6f5f1;
  --surface: #ffffff;
  --ink: #1d2024;
  --muted: #646a72;
  --line: #dcdad3;
  --accent: #2f5d8a;
  --on-accent: #ffffff;
  --new: #1f63b5;
  --learn: #b5402f;
  --review: #1f7a4d;
  --danger: #b3261e;
  --seq-0: #e9e7e0;
  --seq-1: #bcd3e8;
  --seq-2: #86afd3;
  --seq-3: #4b82b5;
  --seq-4: #2a5a8a;
  --bar: #4b82b5;
  --mark: #fbe7a6;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --surface: #202327;
    --ink: #eceef0;
    --muted: #a1a7af;
    --line: #363a40;
    --accent: #7fb0dd;
    --on-accent: #10202f;
    --new: #7fb4ee;
    --learn: #ee9384;
    --review: #72cf9f;
    --danger: #f2978f;
    --seq-0: #2b2f34;
    --seq-1: #2f4a64;
    --seq-2: #3f6c94;
    --seq-3: #6195c4;
    --seq-4: #a5cbec;
    --bar: #6195c4;
    --mark: #5b4a12;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic UI", "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

a { color: var(--accent); }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand { font-weight: 700; font-size: 18px; color: var(--ink); text-decoration: none; }

.sync-chip { font-size: 13px; color: var(--muted); text-decoration: none; text-align: right; }
.sync-chip.error { color: var(--danger); font-weight: 600; }

main {
  flex: 1;
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 16px;
}

.nav {
  position: sticky;
  bottom: 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}

.nav a {
  flex: 1;
  padding: 12px 2px;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
  text-decoration: none;
}

.nav a.active { color: var(--accent); font-weight: 700; }

h1 { font-size: 22px; margin: 4px 0 14px; }
h2 { font-size: 17px; margin: 24px 0 10px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px;
  margin-bottom: 14px;
}

.muted { color: var(--muted); font-size: 14px; }
.error-text { color: var(--danger); font-size: 14px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spread { justify-content: space-between; }

button, .button {
  appearance: none;
  min-height: 44px;
  padding: 8px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}

button.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }
button.danger { color: var(--danger); }
button.quiet { background: transparent; border-color: transparent; color: var(--muted); padding: 8px 10px; }
button:disabled { opacity: .45; cursor: default; }

label { display: block; margin-bottom: 12px; font-size: 14px; color: var(--muted); }

input, textarea, select {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 16px; /* iOS の自動ズームを防ぐ */
}

textarea { min-height: 96px; resize: vertical; }
input[type="checkbox"] { display: inline-block; width: auto; margin: 0 8px 0 0; }
input[type="file"] { padding: 8px; }

/* デッキ一覧 */
.deck {
  display: flex;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  margin-bottom: 10px;
}

.deck-main {
  flex: 1;
  min-width: 0;
  min-height: 60px;
  padding: 10px 14px;
  border: 0;
  background: none;
  text-align: left;
}

.deck-name { display: block; font-weight: 600; overflow-wrap: anywhere; }
.counts { display: flex; gap: 12px; font-size: 14px; font-variant-numeric: tabular-nums; }
.c-new { color: var(--new); }
.c-learn { color: var(--learn); }
.c-review { color: var(--review); }

/* 学習 */
.study { display: flex; flex-direction: column; min-height: calc(100dvh - 90px - env(safe-area-inset-top)); }
body.studying .nav { display: none; }
body.studying main { display: flex; flex-direction: column; padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
.study { flex: 1; min-height: 0; }

.study-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 16px;
  margin: 10px 0;
  padding: 24px 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  text-align: center;
  overflow-wrap: anywhere;
}

.face { font-size: 24px; white-space: pre-wrap; }
.face.back { font-size: 21px; border-top: 1px solid var(--line); padding-top: 16px; }
.speak { align-self: center; }

.answers { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }

.answers button {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 2px;
  min-height: 62px;
  font-weight: 600;
  line-height: 1.3;
}

.answers small { font-weight: 400; font-size: 12px; color: var(--muted); }
.reveal { width: 100%; min-height: 62px; }
.hint { text-align: center; }

@media (hover: none) { .hint { display: none; } }

.answer-word { font-size: 24px; font-weight: 700; }
.meaning { font-size: 19px; }
.definition { font-size: 15px; color: var(--muted); }
.source { font-size: 13px; color: var(--muted); }
.sentence { font-size: 20px; line-height: 1.6; text-align: left; white-space: pre-wrap; }
.sentence mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 2px; font-weight: 600; }
.blank { display: inline-block; min-width: 72px; border-bottom: 2px solid var(--accent); color: var(--accent); font-weight: 600; text-align: left; }
.reveal-block { display: flex; flex-direction: column; gap: 8px; border-top: 1px solid var(--line); padding-top: 14px; }

/* 登録 */
.tokens { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 12px; line-height: 1.3; }
.token { min-height: 38px; padding: 4px 8px; font-size: 16px; border-radius: 6px; background: var(--bg); }
.token.selected { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.known { border: 1px solid var(--accent); border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; }
.known b { font-size: 17px; }
details { margin-bottom: 12px; }
summary { cursor: pointer; color: var(--muted); font-size: 14px; min-height: 28px; }
.badge { font-size: 12px; color: var(--muted); border: 1px solid var(--line); border-radius: 10px; padding: 0 7px; white-space: nowrap; }

/* 読書 */
.timer { font-size: 44px; font-weight: 700; font-variant-numeric: tabular-nums; text-align: center; margin: 4px 0 12px; }
.book-row { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-top: 1px solid var(--line); }
.book-row:first-child { border-top: 0; }
.book-row b { overflow-wrap: anywhere; font-weight: 600; }
.book-row span { font-size: 14px; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }

/* 一覧 */
.card-row {
  display: block;
  width: 100%;
  min-height: 56px;
  margin-bottom: 8px;
  padding: 10px 14px;
  text-align: left;
  border-radius: 10px;
}

.card-row b, .card-row span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-row b { font-weight: 600; }
.card-row span { color: var(--muted); font-size: 14px; }

/* 統計 */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin-bottom: 14px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; }
.tile b { display: block; font-size: 26px; font-variant-numeric: tabular-nums; line-height: 1.25; }
.tile span { font-size: 13px; color: var(--muted); }

.heatmap { display: grid; grid-auto-flow: column; grid-template-rows: repeat(7, 1fr); gap: 3px; }
.heat-cell { aspect-ratio: 1; border-radius: 3px; background: var(--seq-0); min-width: 0; }
.heat-cell[data-level="1"] { background: var(--seq-1); }
.heat-cell[data-level="2"] { background: var(--seq-2); }
.heat-cell[data-level="3"] { background: var(--seq-3); }
.heat-cell[data-level="4"] { background: var(--seq-4); }
.legend { display: flex; align-items: center; justify-content: flex-end; gap: 4px; margin-top: 8px; font-size: 12px; color: var(--muted); }
.legend .heat-cell { width: 12px; }

.bars { display: flex; align-items: flex-end; gap: 2px; height: 120px; border-bottom: 1px solid var(--line); }
.bar-slot { flex: 1; height: 100%; display: flex; align-items: flex-end; min-width: 0; }
.bars.narrow .bar-slot { max-width: 32px; }
.bar { width: 100%; min-height: 0; background: var(--bar); border-radius: 4px 4px 0 0; }
.bar-axis { display: flex; justify-content: space-between; margin-top: 4px; font-size: 12px; color: var(--muted); }
.chart-readout { min-height: 20px; margin-top: 6px; font-size: 13px; color: var(--muted); }

dialog {
  width: min(560px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ink);
  padding: 18px;
}

dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog h2 { margin-top: 0; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(76px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 9px 16px;
  border-radius: 8px;
  background: var(--ink);
  color: var(--bg);
  font-size: 14px;
  z-index: 10;
}
