:root {
  --bg: #171a24;
  --panel: #1f2333;
  --panel-2: #262b3d;
  --border: #343a52;
  --text: #e6e9f2;
  --muted: #8a92ad;
  --accent: #7cc4ff;
  --accent-2: #ffd479;
  --ok: #6fdc8c;
  --bad: #ff6b7a;
  --key: #2b3148;
  --key-text: #f0f2f8;
  --u: clamp(34px, 5.6vw, 58px);
  --gap: calc(var(--u) * 0.11);
  --f-lp: #c77dff;
  --f-lr: #6fa8ff;
  --f-lm: #55d6c2;
  --f-li: #9be36d;
  --f-ri: #ffd166;
  --f-rm: #ff9f5a;
  --f-rr: #ff7b9c;
  --f-rp: #d98cff;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; padding: 0; }

body {
  touch-action: manipulation; /* ダブルタップ拡大を防ぐ */
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Hiragino Sans", "Noto Sans JP", system-ui, sans-serif;
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  padding: 14px 20px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

h1 {
  margin: 0 0 8px;
  font-size: 1.35rem;
  letter-spacing: 0.02em;
}
h1 .sub {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: normal;
  margin-left: 10px;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  font-size: 0.9rem;
}
.controls label { color: var(--muted); }
.controls .spacer { flex: 1; }
.controls .chk { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }
.controls .volume input[type="range"] { width: 90px; accent-color: var(--accent); cursor: pointer; }
.controls .volume-value { min-width: 2ch; text-align: right; font-variant-numeric: tabular-nums; }

select, button, input[type="text"], input:not([type]) {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
}
button { cursor: pointer; }
button:hover { border-color: var(--accent); }
button.ghost-btn { background: transparent; color: var(--muted); }
button.ghost-btn:hover { color: var(--text); }

.layout-panel {
  margin-top: 10px;
  padding: 10px 12px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.85rem;
}
.layout-panel p { margin: 0 0 8px; color: var(--muted); }
.layout-inputs { display: flex; flex-direction: column; gap: 6px; max-width: 320px; }
.layout-inputs input {
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 1.1rem;
  letter-spacing: 0.35em;
  text-transform: uppercase;
}
.layout-actions { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.error { color: var(--bad); }

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 20px 8px;
  gap: 14px;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
}

/* ---- 統計 ---- */
.stats { width: 100%; }
.progress-wrap {
  height: 6px;
  background: var(--panel-2);
  border-radius: 3px;
  overflow: hidden;
}
.progress-bar {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--accent), var(--ok));
  transition: width 0.15s ease-out;
}
.stat-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 24px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.9rem;
}
.stat-row b {
  color: var(--text);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

/* ---- お題 ---- */
.text {
  width: 100%;
  min-height: 7.5rem;
  padding: 18px 22px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 6px;
}
.text .placeholder { color: var(--muted); text-align: center; font-size: 1.2rem; }
.ja-display { font-size: 1.5rem; }
.ja-kana { font-size: 1.05rem; color: var(--muted); letter-spacing: 0.05em; }
.ja-kana .done { color: var(--ok); }
.ja-kana .cur { color: var(--accent-2); text-decoration: underline; text-underline-offset: 3px; }

.line {
  font-family: ui-monospace, "Cascadia Mono", Consolas, "Roboto Mono", monospace;
  font-size: clamp(1.25rem, 2.4vw, 1.9rem);
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-word;
  letter-spacing: 0.02em;
}
.line .done { color: var(--ok); }
.line .todo { color: var(--muted); }
.line .cur {
  color: var(--text);
  background: rgba(124, 196, 255, 0.22);
  border-radius: 3px;
  box-shadow: inset 0 -3px 0 var(--accent);
  animation: blink 1.1s steps(2, start) infinite;
}
.line .cur.space { box-shadow: inset 0 -3px 0 var(--accent), inset 0 0 0 1px rgba(124,196,255,0.4); }
@keyframes blink { 50% { box-shadow: inset 0 -3px 0 transparent; } }

.text.shake { animation: shake 0.18s ease-in-out; }
@keyframes shake {
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

.ime-warning {
  margin: -6px 0 0;
  padding: 6px 12px;
  background: rgba(255, 107, 122, 0.15);
  border: 1px solid var(--bad);
  color: var(--bad);
  border-radius: 6px;
  font-size: 0.9rem;
}

/* ---- キーボード ---- */
.keyboard {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: calc(var(--u) * 0.28);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  user-select: none;
}
.kb-row { display: flex; gap: var(--gap); }
.key {
  --w: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  position: relative;
  width: calc(var(--u) * var(--w) - var(--gap));
  height: calc(var(--u) - var(--gap));
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--key);
  color: var(--key-text);
  border-radius: calc(var(--u) * 0.14);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.06);
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: calc(var(--u) * 0.42);
  font-weight: 600;
  transition: background 0.08s, transform 0.08s, box-shadow 0.08s;
}
.key .main { line-height: 1; }
.key .shifted {
  position: absolute;
  top: 8%;
  left: 12%;
  font-size: calc(var(--u) * 0.22);
  color: var(--muted);
  font-weight: 500;
}
.key .qwerty {
  position: absolute;
  right: 10%;
  bottom: 6%;
  font-size: calc(var(--u) * 0.2);
  color: var(--muted);
  font-weight: 500;
  display: none;
}
.keyboard.show-qwerty .key .qwerty { display: block; }
/* 文字を隠す（Tab）。visibility なので箱の大きさは変わらずレイアウトは崩れない */
.keyboard.hide-labels .key[data-code] .main,
.keyboard.hide-labels .key[data-code] .shifted,
.keyboard.hide-labels .key[data-code] .qwerty { visibility: hidden; }

.key.ghost, .key.mod, .key.space {
  font-size: calc(var(--u) * 0.22);
  font-weight: 500;
  font-family: inherit;
}
.key.ghost { background: transparent; color: transparent; box-shadow: none; border: 1px dashed var(--border); }
.key.mod, .key.space { background: #242a3f; color: var(--muted); }
.key.mod.armed { background: var(--accent-2); color: #0b1220; }
.key.ghost { cursor: default; }
.key.home-bump::after {
  content: "";
  position: absolute;
  bottom: 12%;
  left: 35%;
  width: 30%;
  height: 2px;
  border-radius: 1px;
  background: rgba(255, 255, 255, 0.35);
}

/* 指の色分け */
.keyboard.show-fingers .key[data-finger="lp"] { background: color-mix(in srgb, var(--f-lp) 22%, var(--key)); }
.keyboard.show-fingers .key[data-finger="lr"] { background: color-mix(in srgb, var(--f-lr) 22%, var(--key)); }
.keyboard.show-fingers .key[data-finger="lm"] { background: color-mix(in srgb, var(--f-lm) 22%, var(--key)); }
.keyboard.show-fingers .key[data-finger="li"] { background: color-mix(in srgb, var(--f-li) 22%, var(--key)); }
.keyboard.show-fingers .key[data-finger="ri"] { background: color-mix(in srgb, var(--f-ri) 22%, var(--key)); }
.keyboard.show-fingers .key[data-finger="rm"] { background: color-mix(in srgb, var(--f-rm) 22%, var(--key)); }
.keyboard.show-fingers .key[data-finger="rr"] { background: color-mix(in srgb, var(--f-rr) 22%, var(--key)); }
.keyboard.show-fingers .key[data-finger="rp"] { background: color-mix(in srgb, var(--f-rp) 22%, var(--key)); }

/* 次に打つキー */
.keyboard .key.next {
  background: var(--accent) !important;
  color: #0b1220;
  box-shadow: 0 0 0 2px rgba(124, 196, 255, 0.5), 0 0 18px 4px rgba(124, 196, 255, 0.55);
  transform: translateY(-1px);
  z-index: 1;
}
.keyboard .key.next .shifted, .keyboard .key.next .qwerty { color: rgba(11, 18, 32, 0.6); }
.keyboard .key.next-shift {
  background: var(--accent-2) !important;
  color: #0b1220;
  box-shadow: 0 0 0 2px rgba(255, 212, 121, 0.5), 0 0 14px 3px rgba(255, 212, 121, 0.45);
}
.keyboard .key.hit {
  background: var(--ok) !important;
  color: #0b1220;
  transform: translateY(2px);
  box-shadow: none;
}
.keyboard .key.miss {
  background: var(--bad) !important;
  color: #fff;
  transform: translateY(2px);
  box-shadow: 0 0 14px 3px rgba(255, 107, 122, 0.6);
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
  text-align: center;
}
kbd {
  display: inline-block;
  min-width: 1.4em;
  padding: 0 5px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--panel-2);
  color: var(--text);
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.85em;
  text-align: center;
}

/* ---- 結果モーダル ---- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(8, 10, 18, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
}
.modal-card {
  width: min(520px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 22px 26px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}
.modal-card h2 { margin: 0 0 6px; }
.modal-card h3 { margin: 14px 0 6px; font-size: 0.95rem; color: var(--muted); }
.result-mode { margin: 0 0 12px; color: var(--muted); font-size: 0.9rem; }
.result-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin: 0;
}
.result-grid div {
  background: var(--panel-2);
  border-radius: 8px;
  padding: 8px 10px;
}
.result-grid dt { font-size: 0.75rem; color: var(--muted); }
.result-grid dd { margin: 0; font-size: 1.4rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.result-best { margin: 12px 0 0; color: var(--accent-2); }
.result-perfect { margin: 12px 0 0; color: var(--ok); }
.worst { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 8px 16px; font-size: 0.9rem; }
.worst .finger { color: var(--muted); font-size: 0.8rem; }
.modal-actions { display: flex; gap: 10px; margin-top: 18px; justify-content: flex-end; }
.modal-actions button { padding: 8px 14px; }
#result-again { background: var(--accent); color: #0b1220; border-color: transparent; font-weight: 600; }

.app-footer {
  text-align: center;
  padding: 8px;
  font-size: 0.8rem;
  color: var(--muted);
}
.app-footer a { color: var(--muted); }

/* ---- スマホ ---- */
.mobile-only { display: none; }

@media (max-width: 720px) {
  :root { --u-compact: calc((100vw - 22px) / 11.1); }
  .mobile-only { display: inline-block; }
  .desktop-only { display: none; }
  .app-header { padding: 8px 10px 8px; }
  h1 { font-size: 1.1rem; }
  h1 .sub { display: block; margin: 2px 0 0; font-size: 0.72rem; }
  .controls { gap: 8px 10px; font-size: 0.85rem; }
  .controls .spacer { display: none; }
  .controls .extra { display: none; }
  .controls.show-extra .extra { display: inline-flex; }
  .controls.show-extra .extra.ghost-btn { display: inline-block; }
  /* キーボードは画面下部に固定。その高さぶん本文の下を空ける */
  main { padding: 10px 8px calc(var(--u-compact) * 6.4 + 24px + env(safe-area-inset-bottom, 0px)); gap: 10px; }
  .app-footer { display: none; }
  .stat-row { gap: 4px 14px; font-size: 0.8rem; }
  .text { min-height: 6rem; padding: 12px 14px; border-radius: 10px; }
  .ja-display { font-size: 1.15rem; }
  .ja-kana { font-size: 0.9rem; }
  .line { font-size: 1.15rem; line-height: 1.6; }
  .result-grid { grid-template-columns: repeat(2, 1fr); }
  .hint { font-size: 0.75rem; }
  .app-footer { padding: 4px; }
}

/* コンパクト配列（ダミーキーなし・Shift は最下段）。幅は画面いっぱいに */
.keyboard.compact {
  --u: var(--u-compact, calc((100vw - 22px) / 11.1));
  --gap: calc(var(--u) * 0.1);
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 5;
  width: 100%;
  padding: calc(var(--u) * 0.28) calc(var(--u) * 0.2) calc(var(--u) * 0.28 + env(safe-area-inset-bottom, 0px));
  border-radius: 12px 12px 0 0;
  border-bottom: none;
  box-shadow: 0 -6px 20px rgba(0, 0, 0, 0.35);
}
.keyboard.compact .key {
  height: calc(var(--u) * 1.35 - var(--gap));
  font-size: calc(var(--u) * 0.48);
}
.keyboard.compact .key .qwerty { font-size: max(9px, calc(var(--u) * 0.2)); }
.keyboard.compact .key .shifted { font-size: max(9px, calc(var(--u) * 0.22)); }
.keyboard.compact .key.mod, .keyboard.compact .key.space { font-size: max(11px, calc(var(--u) * 0.28)); }
