:root {
  color-scheme: light dark;
  --bg1: #0f172a;
  --bg2: #1e1b4b;
  --fg: #f4f4f5;
  --muted: #b6bcc9;
  --accent: #fbbf24;
  --accent-fg: #1c1917;
  --good: #4ade80;
  --bad: #fb7185;
  --warn: #fb923c;
  --card: rgba(255, 255, 255, 0.06);
  --card-border: rgba(255, 255, 255, 0.12);
  --chip: rgba(255, 255, 255, 0.08);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg1: #fdf8ef;
    --bg2: #fceede;
    --fg: #1f2430;
    --muted: #5b6270;
    --accent: #d97706;
    --accent-fg: #ffffff;
    --good: #15803d;
    --bad: #dc2626;
    --warn: #ea580c;
    --card: #ffffff;
    --card-border: #e8e0d2;
    --chip: #f4ede1;
    --shadow: 0 10px 24px rgba(120, 90, 40, 0.12);
  }
}

* { box-sizing: border-box; }
html { height: 100%; }
body {
  margin: 0;
  min-height: 100%;
  font-family: "M PLUS Rounded 1c", system-ui, -apple-system, "Segoe UI", Roboto,
    "Noto Sans JP", "Hiragino Kaku Gothic ProN", "Meiryo", sans-serif;
  font-size: 18px;
  line-height: 1.6;
  color: var(--fg);
  background:
    radial-gradient(1200px 600px at 85% -10%, rgba(251, 191, 36, 0.14), transparent 60%),
    radial-gradient(900px 500px at -10% 100%, rgba(96, 165, 250, 0.12), transparent 60%),
    linear-gradient(160deg, var(--bg1), var(--bg2));
  background-attachment: fixed;
  display: flex;
  flex-direction: column;
}

/* ===== ヘッダー ===== */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding: 16px clamp(12px, 4vw, 32px);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-icon { font-size: 1.8rem; }
.app-header h1 {
  margin: 0;
  font-size: clamp(1.4rem, 4vw, 1.9rem);
  font-weight: 900;
  letter-spacing: 0.06em;
  background: linear-gradient(90deg, var(--accent), #f59e0b 60%, #f97316);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.header-actions { display: flex; gap: 10px; }

button {
  font-family: inherit;
  font-size: 1rem;
  cursor: pointer;
  border-radius: 12px;
  border: 2px solid var(--card-border);
  background: var(--chip);
  color: var(--fg);
  padding: 10px 16px;
  min-height: 48px;
  transition: transform 120ms ease, filter 120ms ease, background 120ms ease;
}
button:hover { filter: brightness(1.1); }
button:active { transform: scale(0.97); }
button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

button.primary {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
  font-weight: 700;
}
.icon-btn { font-weight: 700; }
.icon-btn[aria-pressed="false"] { opacity: 0.55; }

/* ===== レイアウト ===== */
.container {
  width: min(860px, 100% - 24px);
  margin: 0 auto;
  display: grid;
  gap: 20px;
  flex: 1;
  padding-bottom: 24px;
}

.card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: clamp(20px, 4vw, 32px);
  backdrop-filter: blur(8px);
}

/* ===== スタート画面 ===== */
.start-card { text-align: center; }
.catch {
  margin: 0 0 20px;
  font-size: clamp(1.1rem, 3vw, 1.35rem);
  font-weight: 700;
}
.catch strong { color: var(--accent); }

.steps {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px;
  margin-bottom: 24px;
}
.step {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--chip);
  border: 1px solid var(--card-border);
  border-radius: 999px;
  padding: 8px 18px 8px 8px;
  font-weight: 700;
  font-size: 1.05rem;
}
.step-num {
  display: inline-grid;
  place-items: center;
  width: 2.1em;
  height: 2.1em;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 900;
  font-size: 1rem;
}

.choice-group {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px 14px;
  margin-bottom: 16px;
}
.choice-label {
  font-weight: 900;
  font-size: 1.1rem;
  color: var(--muted);
  min-width: 4em;
}
.segment { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.segment button {
  font-size: 1.1rem;
  font-weight: 700;
  padding: 10px 20px;
}
.segment button[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
  box-shadow: 0 4px 14px rgba(251, 191, 36, 0.35);
}

.big-start {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  margin-top: 12px;
  padding: 18px 56px;
  font-size: clamp(1.5rem, 4.5vw, 1.9rem);
  font-weight: 900;
  letter-spacing: 0.08em;
  color: var(--accent-fg);
  background: linear-gradient(135deg, var(--accent), #f97316);
  border: none;
  border-radius: 999px;
  box-shadow: 0 8px 24px rgba(249, 115, 22, 0.4);
}
.big-start:hover { transform: translateY(-2px); filter: brightness(1.05); }
.big-start .sub {
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  opacity: 0.85;
}
.best-line { margin-top: 18px; color: var(--muted); font-weight: 700; }
.best-line strong { color: var(--accent); font-size: 1.3em; }

/* ===== ゲーム画面 ===== */
.game-card { padding-top: 0; overflow: hidden; }
.timer {
  position: relative;
  height: 14px;
  margin: 0 calc(-1 * clamp(20px, 4vw, 32px)) 16px;
  background: rgba(127, 127, 127, 0.25);
}
.timer-bar {
  position: absolute;
  inset: 0 auto 0 0;
  width: 100%;
  background: linear-gradient(90deg, var(--good), #2dd4bf);
  transition: width 100ms linear, background 200ms;
}

.hud {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px 18px;
  margin-bottom: 18px;
}
.hud-item {
  display: flex;
  align-items: baseline;
  gap: 6px;
  background: var(--chip);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: 8px 16px;
}
.hud-label { color: var(--muted); font-weight: 700; font-size: 0.95rem; }
.hud-value {
  font-size: 1.6rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}
.hud-unit { color: var(--muted); font-weight: 700; font-size: 0.95rem; }
.hud-time .hud-value { color: var(--accent); }
.hud-time.low .hud-value { color: var(--bad); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: 0.5; } }

.word {
  text-align: center;
  padding: 20px 12px 26px;
  border-radius: 16px;
  min-height: 220px;
  display: grid;
  place-content: center;
  gap: 8px;
}
.word .label {
  font-size: clamp(1.3rem, 4vw, 2rem);
  font-weight: 700;
  color: var(--muted);
}
.word .reading {
  font-size: clamp(2.2rem, 7vw, 3.6rem);
  font-weight: 900;
  letter-spacing: 0.04em;
  line-height: 1.35;
}
.word .hint-title {
  margin-top: 10px;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--muted);
}
.word .romaji {
  font-size: clamp(1.6rem, 5vw, 2.4rem);
  font-weight: 900;
  letter-spacing: 0.12em;
  font-variant-numeric: tabular-nums;
}
.romaji .rdone { color: var(--good); }
.romaji .rnext {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 6px;
  text-decoration-thickness: 4px;
}
.romaji .rrest { color: var(--muted); }
.romaji.err .rnext { color: var(--bad); text-decoration-color: var(--bad); }

.word .idle-msg { font-size: 1.3rem; color: var(--muted); font-weight: 700; }

.rchar { opacity: 0.55; }
.rchar.correct { color: var(--good); opacity: 1; }
.rchar.current {
  position: relative;
  opacity: 1;
  color: var(--accent);
}
.rchar.current::after {
  content: "";
  position: absolute;
  left: 2%;
  right: 2%;
  bottom: -6px;
  height: 5px;
  border-radius: 3px;
  background: var(--accent);
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink { 50% { opacity: 0.25; } }
.rchar.error { color: var(--bad); }
.rchar.error::after { background: var(--bad); animation: none; }

.paused {
  text-align: center;
  color: var(--warn);
  font-weight: 900;
  font-size: 1.2rem;
  padding: 10px;
  cursor: pointer;
}

.game-actions {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 8px;
}

.shake { animation: shake 200ms linear; }
@keyframes shake {
  0% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  50% { transform: translateX(6px); }
  75% { transform: translateX(-4px); }
  100% { transform: translateX(0); }
}

.glow { animation: glow 300ms ease-out; }
@keyframes glow {
  from { text-shadow: 0 0 0 rgba(251, 191, 36, 0); }
  to { text-shadow: 0 0 22px rgba(251, 191, 36, 0.6); }
}

/* ===== ランキング ===== */
.leaderboard { padding-top: 16px; padding-bottom: 16px; }
.lb-title { font-weight: 900; font-size: 1.1rem; margin-bottom: 6px; }
.highscores-list {
  margin: 6px 0 0;
  padding-left: 1.6em;
  font-size: 1.05rem;
  font-weight: 700;
}
.highscores-list li { line-height: 1.7; }

/* ===== フッター ===== */
.app-footer {
  text-align: center;
  color: var(--muted);
  font-size: 0.95rem;
  padding: 14px 16px 22px;
}

/* ===== ダイアログ ===== */
dialog {
  border: 1px solid var(--card-border);
  background: color-mix(in srgb, var(--bg1) 88%, white 12%);
  color: var(--fg);
  border-radius: 20px;
  padding: clamp(20px, 4vw, 32px);
  width: min(480px, 92vw);
  box-shadow: var(--shadow);
}
@media (prefers-color-scheme: light) {
  dialog { background: #ffffff; }
}
dialog::backdrop { background: rgba(10, 10, 20, 0.6); backdrop-filter: blur(3px); }
dialog form { display: grid; gap: 16px; }
dialog h2 { margin: 0 0 4px; font-size: 1.4rem; }
dialog label {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  font-weight: 700;
}
dialog input[type="checkbox"] { width: 24px; height: 24px; }
dialog input[type="range"] { width: 180px; }
dialog menu {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin: 0;
  padding: 0;
}
.button-like {
  display: inline-block;
  padding: 10px 16px;
  background: var(--chip);
  border: 2px solid var(--card-border);
  border-radius: 12px;
  cursor: pointer;
  font-weight: 700;
}
.import small { display: block; margin-top: 6px; color: var(--muted); }

/* ===== 結果 ===== */
.result-dialog { text-align: center; }
.result-emoji { font-size: 3.2rem; line-height: 1; }
.result h2 { margin: 8px 0 12px; font-size: 1.5rem; }
.result-main { margin-bottom: 14px; }
.result-number {
  font-size: 4rem;
  font-weight: 900;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.result-unit { font-size: 1.2rem; font-weight: 700; margin-left: 6px; }
.name-row {
  background: var(--chip);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: 12px 16px;
  margin-bottom: 14px;
  font-weight: 700;
}
.name-row label { display: block; margin-bottom: 8px; }
.name-line { display: flex; gap: 8px; justify-content: center; }
.name-line input {
  font-family: inherit;
  font-size: 1.1rem;
  font-weight: 700;
  padding: 8px 12px;
  border-radius: 10px;
  border: 2px solid var(--card-border);
  background: var(--card);
  color: var(--fg);
  width: min(240px, 60%);
}
.name-line input:focus-visible { outline: 3px solid var(--accent); outline-offset: 1px; }

.result .rows {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}
.row-item {
  background: var(--chip);
  border: 1px solid var(--card-border);
  border-radius: 12px;
  padding: 8px 14px;
  display: flex;
  gap: 8px;
  align-items: baseline;
}
.row-item span { color: var(--muted); font-size: 0.95rem; font-weight: 700; }
.row-item strong { font-size: 1.2rem; }
.result .highscores h3 { margin: 8px 0 4px; font-size: 1.05rem; }
.result .highscores-list { text-align: left; }
.result menu { justify-content: center; margin-top: 16px; flex-wrap: wrap; }
.result .primary { font-size: 1.2rem; padding: 12px 24px; }

/* ===== トースト ===== */
.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 900;
  padding: 12px 24px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  z-index: 10000;
}

.fx-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 9999;
}

@media (max-width: 560px) {
  body { font-size: 16px; }
  .hud-value { font-size: 1.3rem; }
  .word { min-height: 180px; }
}
