:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #efebe4;
  --text: #1d1d1f;
  --muted: #6b6760;
  --border: #e2ddd4;
  --accent: #3b4a8f;
  --accent-soft: #dfe3f3;
  --correct: #23865a;
  --correct-press: #1b6c48;
  --incorrect: #c9413a;
  --incorrect-press: #a8332d;
  --shadow: 0 10px 30px rgba(40, 32, 20, 0.12), 0 2px 6px rgba(40, 32, 20, 0.08);
  --radius: 22px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-ja: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Noto Sans CJK JP", "Yu Gothic", "YuGothic", "Meiryo", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14151a;
    --surface: #23252d;
    --surface-2: #2c2f38;
    --text: #efece6;
    --muted: #a19d95;
    --border: #363945;
    --accent: #8b9cf0;
    --accent-soft: #2e3550;
    --correct: #2f9d6b;
    --correct-press: #267f57;
    --incorrect: #d4524a;
    --incorrect-press: #b3423b;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.45), 0 2px 6px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
  }
}

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

html, body {
  margin: 0;
  height: 100%;
  overflow-x: hidden;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button, select {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
}

button { cursor: pointer; }

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.app {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  max-width: 560px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding:
    max(14px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
}

/* Top bar */

.top {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.top-row {
  display: flex;
  gap: 10px;
}

.deck-picker { flex: 1; min-width: 0; }

.icon-btn {
  flex: none;
  width: 50px;
  padding: 0;
  font-size: 1.35rem;
  line-height: 1;
}

.deck-picker select {
  width: 100%;
  min-height: 50px;
  padding: 0 44px 0 16px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background:
    var(--surface)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='9' viewBox='0 0 14 9'%3E%3Cpath d='M1 1l6 6 6-6' fill='none' stroke='%23888' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E")
    no-repeat right 16px center;
  font-size: 1.05rem;
  font-weight: 600;
  appearance: none;
  -webkit-appearance: none;
}

.progress-text {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
  font-size: 1rem;
  font-variant-numeric: tabular-nums;
}

#completed-text { font-weight: 700; }
#remaining-text { color: var(--muted); }

.total-time {
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
}

.total-time[hidden], .done-time[hidden] { display: none; }

.done-time {
  margin: -12px 0 0;
  color: var(--muted);
  font-size: 1.1rem;
  font-variant-numeric: tabular-nums;
}

.progress-bar {
  height: 10px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}

.progress-fill {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width 300ms ease;
}

.tools {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.tool {
  min-height: 50px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  font-size: 1.05rem;
  font-weight: 600;
}

.tool:active { background: var(--surface-2); }
.tool:disabled { opacity: 0.45; cursor: default; }

/* Card */

.stage {
  flex: 1;
  display: flex;
  min-height: 260px;
}

.study, .done {
  flex: 1;
  display: flex;
}

.study { position: relative; }

.stopwatch {
  position: absolute;
  top: 14px;
  right: 16px;
  z-index: 1;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

.stopwatch[hidden] { display: none; }

.study[hidden], .done[hidden] { display: none; }

.card {
  flex: 1;
  display: block;
  padding: 0;
  border: 0;
  background: none;
  perspective: 1400px;
  user-select: none;
  -webkit-user-select: none;
  animation: card-in 260ms ease-out;
}

.card.entering { animation: none; }

@keyframes card-in {
  from { opacity: 0; transform: translateY(10px) scale(0.985); }
  to { opacity: 1; transform: none; }
}

.card-inner {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 450ms cubic-bezier(0.2, 0.7, 0.2, 1);
}

.card.is-flipped .card-inner { transform: rotateY(180deg); }
.card-inner.no-anim { transition: none; }

.face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 28px 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: center;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.back { transform: rotateY(180deg); }

.dict, .polite, .back-ja {
  font-family: var(--font-ja);
  line-height: 1.35;
  word-break: keep-all;
  overflow-wrap: anywhere;
}

.dict {
  display: block;
  font-size: clamp(1.9rem, 9.5vw, 3.1rem);
  font-weight: 700;
}

.dict .seg { display: inline-block; white-space: nowrap; }
.dict .sep { color: var(--muted); font-weight: 400; }

.polite {
  display: block;
  font-size: clamp(1.6rem, 8vw, 2.5rem);
  font-weight: 500;
}

.polite[hidden] { display: none; }

.hint {
  position: absolute;
  bottom: 16px;
  left: 0;
  right: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.english {
  display: block;
  font-size: clamp(1.7rem, 8vw, 2.6rem);
  font-weight: 700;
  line-height: 1.25;
  overflow-wrap: anywhere;
}

.back-ja {
  display: block;
  color: var(--muted);
  font-size: clamp(1.05rem, 4.8vw, 1.35rem);
}

/* Completion */

.done {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 28px;
  padding: 32px 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: center;
}

.done h2 {
  margin: 0;
  font-size: clamp(1.6rem, 7vw, 2.2rem);
  line-height: 1.3;
}

.big-btn {
  width: 100%;
  max-width: 320px;
  min-height: 64px;
  border: 0;
  border-radius: 16px;
  background: var(--accent);
  color: #fff;
  font-size: 1.25rem;
  font-weight: 700;
}

@media (prefers-color-scheme: dark) {
  .big-btn { color: #14151a; }
}

/* Answer buttons */

.answers {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.answers[hidden] { display: none; }

.answer-btn {
  min-height: 68px;
  border: 0;
  border-radius: 18px;
  color: #fff;
  font-size: 1.25rem;
  font-weight: 700;
  transition: transform 80ms ease, background-color 120ms ease;
}

.answer-btn:active { transform: scale(0.97); }
.correct { background: var(--correct); }
.correct:active { background: var(--correct-press); }
.incorrect { background: var(--incorrect); }
.incorrect:active { background: var(--incorrect-press); }

@media (hover: hover) {
  .tool:hover { background: var(--surface-2); }
  .correct:hover { background: var(--correct-press); }
  .incorrect:hover { background: var(--incorrect-press); }
}

/* Settings */

.settings {
  width: 100%;
  max-width: 560px;
  max-height: 100%;
  margin: 0 auto;
  padding: 0;
  border: 0;
  border-radius: 0 0 var(--radius) var(--radius);
  background: var(--bg);
  color: var(--text);
  box-shadow: var(--shadow);
}

.settings::backdrop { background: rgba(0, 0, 0, 0.35); }

.settings-inner {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding:
    max(14px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    20px
    max(16px, env(safe-area-inset-left));
}

.settings-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.settings-head h2 { margin: 0; font-size: 1.5rem; }
.settings-head .tool { padding: 0 20px; }

.setting {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  cursor: pointer;
}

.setting-text { display: flex; flex-direction: column; gap: 4px; }
.setting-name { font-size: 1.05rem; font-weight: 600; }
.setting-desc { color: var(--muted); font-size: 0.9rem; }

.switch {
  flex: none;
  position: relative;
  width: 52px;
  height: 32px;
  margin: 0;
  border-radius: 999px;
  background: var(--border);
  appearance: none;
  -webkit-appearance: none;
  cursor: pointer;
  transition: background-color 150ms ease;
}

.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 150ms ease;
}

.switch:checked { background: var(--correct); }
.switch:checked::after { transform: translateX(20px); }

.setting-cap {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.setting-cap legend { padding: 0; margin-bottom: 10px; }
.setting-cap:disabled, .setting-row.is-disabled { opacity: 0.45; }
.setting-row.is-disabled, .switch:disabled { cursor: default; }

.segmented {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 4px;
  border-radius: 12px;
  background: var(--surface-2);
}

.segmented label { position: relative; }

.segmented input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.segmented span {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  border-radius: 9px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.segmented input:checked + span {
  background: var(--surface);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}

.segmented input:focus-visible + span {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.setting-cap:disabled input { cursor: default; }

/* Short phones: tighten spacing so the card keeps its room */
@media (max-height: 640px) {
  .app { gap: 10px; }
  .top { gap: 10px; }
  .deck-picker select, .tool { min-height: 46px; }
  .answer-btn { min-height: 60px; }
  .stage { min-height: 220px; }
}

@media (prefers-reduced-motion: reduce) {
  .card-inner, .progress-fill, .answer-btn, .switch, .switch::after { transition: none; }
  .card { animation: none; }
}
