/* ===========================================================
   style.css — 오방색(五方色)を下敷きにしたトークン設計
   청=青緑(正解/アクセント) 적=朱(ミス) 황=黄(コンボ)
   =========================================================== */

:root {
  /* --- ライト：韓紙(한지)の面 --- */
  --bg:        #E7EAE7;
  --bg-tint:   #DDE2DE;
  --surface:   #FBFCFB;
  --surface-2: #F1F4F1;
  --line:      #C6CDC7;
  --line-soft: #DCE2DD;

  --ink:    #12181A;
  --ink-2:  #4C5754;
  --ink-3:  #7E8A86;

  --cheong:      #0C7567;
  --cheong-deep: #095247;
  --cheong-wash: #D3EAE4;
  --jeok:        #C13627;
  --jeok-wash:   #F6DDD8;
  --hwang:       #A67908;

  --key-face:  #FFFFFF;
  --key-edge:  #C3CAC4;
  --key-shadow: 0 2px 0 var(--key-edge);
  --key-done:  #EDF2EF;

  --next-glow: none;
  --stage-glow: none;

  --radius:  10px;
  --radius-s: 6px;

  --font-ko: 'Gothic A1', 'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif;
  --font-ja: 'Zen Kaku Gothic New', 'Hiragino Sans', 'Noto Sans JP', sans-serif;
  --font-mo: 'IBM Plex Mono', ui-monospace, 'SFMono-Regular', monospace;

  /* 指の色相（低彩度で静かに） */
  --f0: 205; --f1: 248; --f2: 286; --f3: 322;
  --f4: 14;  --f5: 38;  --f6: 96;  --f7: 162;
  --finger-alpha: 0.5;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:        #0A1012;
    --bg-tint:   #070C0E;
    --surface:   #121B1E;
    --surface-2: #172226;
    --line:      #253337;
    --line-soft: #1B272B;

    --ink:    #E3ECE8;
    --ink-2:  #9AAAA5;
    --ink-3:  #6A7B76;

    --cheong:      #46D8BB;
    --cheong-deep: #8FF0DB;
    --cheong-wash: #0E3A34;
    --jeok:        #FF6E55;
    --jeok-wash:   #3A1712;
    --hwang:       #F3C450;

    --key-face:  #162125;
    --key-edge:  #0A1214;
    --key-shadow: 0 2px 0 var(--key-edge);
    --key-done:  #0E1A1C;

    --next-glow: 0 0 0 1px var(--cheong), 0 0 18px -2px var(--cheong);
    --stage-glow: 0 0 40px -18px var(--cheong);
    --finger-alpha: 0.55;
  }
}

:root[data-theme="dark"] {
  --bg:        #0A1012;
  --bg-tint:   #070C0E;
  --surface:   #121B1E;
  --surface-2: #172226;
  --line:      #253337;
  --line-soft: #1B272B;

  --ink:    #E3ECE8;
  --ink-2:  #9AAAA5;
  --ink-3:  #6A7B76;

  --cheong:      #46D8BB;
  --cheong-deep: #8FF0DB;
  --cheong-wash: #0E3A34;
  --jeok:        #FF6E55;
  --jeok-wash:   #3A1712;
  --hwang:       #F3C450;

  --key-face:  #162125;
  --key-edge:  #0A1214;
  --key-shadow: 0 2px 0 var(--key-edge);
  --key-done:  #0E1A1C;

  --next-glow: 0 0 0 1px var(--cheong), 0 0 18px -2px var(--cheong);
  --stage-glow: 0 0 40px -18px var(--cheong);
  --finger-alpha: 0.55;
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ja);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 1080px;
  margin: 0 auto;
  padding: 20px;
  padding-block: 20px 36px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* ---------- ヘッダ ---------- */
.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }

.brand h1 {
  font-family: var(--font-ko);
  font-weight: 900;
  font-size: 23px;
  letter-spacing: 0.02em;
  margin: 0;
  color: var(--ink);
}
.brand h1 .dot { color: var(--cheong); }

.brand .sub {
  font-size: 12px;
  color: var(--ink-3);
  letter-spacing: 0.08em;
}

.toggles { display: flex; gap: 7px; flex-wrap: wrap; align-items: center; }

/* アイコンだけの切り替えボタン。意味は title と aria-label が持ちます */
.toggle {
  appearance: none;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-3);
  border-radius: 50%;
  padding: 0;
  cursor: pointer;
  transition: border-color .15s, color .15s, background .15s;
}
.toggle svg { display: block; }
.toggle:hover { border-color: var(--ink-3); color: var(--ink); }
.toggle.unavailable {
  border-style: dashed;
  opacity: .5;
}
.toggle[aria-pressed="true"] {
  border-color: var(--cheong);
  color: var(--cheong);
  background: var(--cheong-wash);
}

/* 読み上げの声を選ぶ */
.voice-pick {
  display: inline-flex;
  align-items: center;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 4px 0 10px;
  background: var(--surface);
  max-width: 190px;
}
.voice-pick select {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--ink-2);
  font-family: var(--font-ja);
  font-size: 12px;
  line-height: 1;
  padding: 0 20px 0 0;
  max-width: 100%;
  cursor: pointer;
  text-overflow: ellipsis;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
                    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: right 9px center, right 5px center;
  background-size: 4px 4px, 4px 4px;
  background-repeat: no-repeat;
}
.voice-pick select:focus { outline: none; }
.voice-pick:focus-within { border-color: var(--cheong); }
.voice-pick.unavailable { opacity: .5; border-style: dashed; }

.toggle:focus-visible, .chip:focus-visible, .btn:focus-visible {
  outline: 2px solid var(--cheong);
  outline-offset: 2px;
}

/* ---------- セット選択 ---------- */
.sets { display: flex; gap: 7px; flex-wrap: wrap; }

.chip {
  appearance: none;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink-2);
  font-family: var(--font-ja);
  font-size: 12.5px;
  padding: 7px 12px;
  border-radius: var(--radius-s);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  transition: border-color .15s, color .15s, background .15s;
}
.chip:hover { border-color: var(--ink-3); color: var(--ink); }
.chip[aria-pressed="true"] {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--bg);
}
.chip .lv {
  font-family: var(--font-mo);
  font-size: 10px;
  letter-spacing: -0.5px;
  color: var(--hwang);
}
.chip[aria-pressed="true"] .lv { color: var(--hwang); }

/* ---------- 成績レール ---------- */
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1px;
  background: var(--line-soft);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  overflow: hidden;
}
.stat {
  background: var(--surface);
  padding: 9px 13px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.stat dt {
  font-size: 10.5px;
  letter-spacing: 0.1em;
  color: var(--ink-3);
  margin: 0;
}
.stat dd {
  margin: 0;
  font-family: var(--font-mo);
  font-variant-numeric: tabular-nums;
  font-size: 21px;
  font-weight: 600;
  line-height: 1.15;
  color: var(--ink);
}
.stat dd .u { font-size: 11px; font-weight: 400; color: var(--ink-3); margin-left: 3px; }
#s-combo.hot { color: var(--hwang); }
#s-acc.low   { color: var(--jeok); }

/* ---------- ステージ ---------- */
.stage {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 26px 20px 22px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  min-height: 268px;
  justify-content: center;
  box-shadow: var(--stage-glow);
}
.stage.shake { animation: shake .16s ease-in-out; }
@keyframes shake {
  0%,100% { transform: translateX(0); }
  25%     { transform: translateX(-5px); }
  75%     { transform: translateX(5px); }
}

/* 音節ブロックの升目 ——
   ハングルは四角に組む文字なので、進捗も升目で表します。
   お手本(.model)は升の真上。重ねないので字形がズレません。        */
.blocks {
  --size: clamp(58px, 13vw, 92px);
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
}

.sq {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
}

.model {
  font-family: var(--font-ko);
  font-weight: 700;
  font-size: calc(var(--size) * 0.33);
  line-height: 1;
  color: var(--ink-3);
  opacity: .62;
  transition: color .14s, opacity .14s;
  min-height: 1em;
}
.sq.current .model { color: var(--ink); opacity: 1; }
.sq.done .model { color: var(--cheong); opacity: .8; }

.blk {
  position: relative;
  width: var(--size);
  height: var(--size);
  border: 1.5px dashed var(--line);
  border-radius: var(--radius-s);
  display: grid;
  place-items: center;
  font-family: var(--font-ko);
  font-weight: 700;
  font-size: calc(var(--size) * 0.6);
  line-height: 1;
  background: transparent;
  transition: border-color .12s, background .12s, transform .12s;
}
.blk .live { color: var(--ink); line-height: 1; }

.sq.gap { align-self: stretch; }
.blk.gap {
  width: calc(var(--size) * 0.34);
  border: none;
  font-size: 20px;
}
.blk.gap .live { color: var(--cheong); }

.blk.done {
  border-style: solid;
  border-color: var(--cheong);
  background: var(--cheong-wash);
}
.blk.done .live { color: var(--cheong-deep); }
.blk.gap.done { background: transparent; }

.blk.current {
  border-style: solid;
  border-color: var(--ink-2);
  background: var(--surface-2);
}
/* まだ何も打っていない升には、打ち込む場所を示すキャレット */
.blk.current.blank::after {
  content: '';
  width: 2px;
  height: 40%;
  border-radius: 1px;
  background: var(--cheong);
  animation: blink 1.05s steps(2, end) infinite;
}
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }

.blk.pop { animation: pop .22s cubic-bezier(.34,1.56,.64,1); }
@keyframes pop {
  0%   { transform: scale(1); }
  45%  { transform: scale(1.14); }
  100% { transform: scale(1); }
}

/* 読み・意味 */
.gloss {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  text-align: center;
}
.yomi-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

/* 発音を聞き直すボタン */
.say {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink-3);
  font-family: var(--font-ko);
  font-weight: 700;
  font-size: 12px;
  line-height: 1;
  padding: 5px 10px 6px;
  border-radius: 999px;
  cursor: pointer;
  transition: color .15s, border-color .15s, background .15s;
}
.say:hover { color: var(--cheong); border-color: var(--cheong); background: var(--cheong-wash); }
.say:focus-visible { outline: 2px solid var(--cheong); outline-offset: 2px; }
.say.unavailable { opacity: .4; }
.say.playing { color: var(--cheong); border-color: var(--cheong); }
.say .say-label { min-width: 3.2em; text-align: left; }

.gloss .yomi {
  font-size: 17px;
  font-weight: 500;
  letter-spacing: 0.06em;
  color: var(--ink);
}
.gloss .ja { font-size: 14px; color: var(--ink-2); }
.gloss .ro {
  font-family: var(--font-mo);
  font-size: 11.5px;
  letter-spacing: 0.06em;
  color: var(--ink-3);
}
.gloss.hidden { visibility: hidden; }

.tape {
  font-family: var(--font-mo);
  font-size: 11px;
  letter-spacing: 0.14em;
  color: var(--ink-3);
  text-transform: uppercase;
}

/* 結果パネル */
.result {
  position: absolute;
  inset: 0;
  background: var(--surface);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px 20px;
  text-align: center;
}
.result h2 {
  font-family: var(--font-ko);
  font-weight: 900;
  font-size: 26px;
  margin: 0;
  color: var(--cheong);
}
.result .grid {
  display: flex;
  gap: 26px;
  flex-wrap: wrap;
  justify-content: center;
}
.result .cell { display: flex; flex-direction: column; gap: 2px; }
.result .cell b {
  font-family: var(--font-mo);
  font-variant-numeric: tabular-nums;
  font-size: 27px;
  font-weight: 600;
  line-height: 1;
}
.result .cell span { font-size: 11px; letter-spacing: .08em; color: var(--ink-3); }
.result .missed {
  font-size: 12.5px;
  color: var(--ink-2);
  max-width: 46ch;
}
.result .missed b { font-family: var(--font-ko); font-weight: 700; color: var(--jeok); }

.btn {
  appearance: none;
  border: none;
  background: var(--ink);
  color: var(--bg);
  font-family: var(--font-ja);
  font-weight: 500;
  font-size: 14px;
  padding: 10px 24px;
  border-radius: 999px;
  cursor: pointer;
}
.btn:hover { background: var(--cheong); color: #fff; }

/* ---------- キーボード ----------
   実機と同じANSI配列。1段 = 15ユニット幅で、キー間の隙間も幅に
   織り込むので、どの段も左右の端がぴたりと揃います。            */
.board {
  --u: clamp(29px, 5.1vw, 50px);
  --gap: clamp(3px, 0.5vw, 6px);
  background: var(--bg-tint);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 13px;
  overflow-x: auto;
}
.board-inner {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  width: max-content;
  margin: 0 auto;
}
.krow { display: flex; gap: var(--gap); }

.key {
  --n: 1;
  position: relative;
  width: calc(var(--u) * var(--n) + var(--gap) * (var(--n) - 1));
  height: var(--u);
  border: 1px solid var(--key-edge);
  border-radius: var(--radius-s);
  background: var(--key-face);
  box-shadow: var(--key-shadow);
  display: grid;
  place-items: center;
  transition: transform .06s, background .12s, border-color .12s, box-shadow .12s;
}

.key .cap {
  font-family: var(--font-ko);
  font-weight: 700;
  font-size: calc(var(--u) * 0.44);
  line-height: 1;
  color: var(--ink);
  transition: color .12s;
}
.key .cap.mono {
  font-family: var(--font-mo);
  font-weight: 400;
  font-size: max(9px, calc(var(--u) * 0.24));
  letter-spacing: 0.04em;
  color: var(--ink-3);
}
.key.sym .cap.mono {
  font-size: max(11px, calc(var(--u) * 0.32));
  color: var(--ink-2);
}

.key .lat {
  position: absolute;
  top: 3px; left: 5px;
  font-family: var(--font-mo);
  font-size: max(8px, calc(var(--u) * 0.19));
  color: var(--ink-3);
  text-transform: uppercase;
  transition: color .12s;
}
.key .alt {
  position: absolute;
  top: 2px; right: 4px;
  font-family: var(--font-ko);
  font-weight: 700;
  font-size: max(9px, calc(var(--u) * 0.26));
  line-height: 1;
  color: var(--ink-3);
  transition: color .12s;
}
.key .alt.mono { font-family: var(--font-mo); font-weight: 400; }

/* 指ガイド（割り当てのあるキーだけ） */
.key.guided::after {
  content: '';
  position: absolute;
  left: 24%; right: 24%; bottom: 3px;
  height: 2px;
  border-radius: 2px;
  background: hsl(var(--fh) 42% 50% / var(--finger-alpha));
  opacity: 0;
  transition: opacity .15s;
}
.board.fingers .key.guided::after { opacity: 1; }

.key.next {
  background: var(--cheong);
  border-color: var(--cheong);
  box-shadow: var(--next-glow), var(--key-shadow);
  animation: breathe 1.3s ease-in-out infinite;
}
.key.next .cap, .key.next .lat, .key.next .alt { color: #fff; }
.key.next::after { opacity: 0 !important; }

:root[data-theme="dark"] .key.next .cap,
:root[data-theme="dark"] .key.next .lat,
:root[data-theme="dark"] .key.next .alt { color: #04211C; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .key.next .cap,
  :root:not([data-theme="light"]) .key.next .lat,
  :root:not([data-theme="light"]) .key.next .alt { color: #04211C; }
}

@keyframes breathe {
  0%,100% { filter: brightness(1); }
  50%     { filter: brightness(1.13); }
}

.key.hit { transform: translateY(2px); box-shadow: none; }
.key.miss { background: var(--jeok-wash); border-color: var(--jeok); }
.key.miss .cap { color: var(--jeok); }

/* ---------- 足もと ---------- */
.foot {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  font-size: 11.5px;
  color: var(--ink-3);
}
.foot kbd {
  font-family: var(--font-mo);
  font-size: 10.5px;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 1px 5px;
  color: var(--ink-2);
  background: var(--surface);
}
.foot .voice-note {
  color: var(--hwang);
  border-bottom: 1px dotted currentColor;
  cursor: help;
}
.foot .diag {
  appearance: none;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink-3);
  font-family: var(--font-ja);
  font-size: 11px;
  padding: 3px 9px;
  border-radius: 999px;
  cursor: pointer;
}
.foot .diag:hover { color: var(--cheong); border-color: var(--cheong); }
.foot .diag-out {
  flex-basis: 100%;
  margin: 0;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  font-family: var(--font-mo);
  font-size: 11px;
  line-height: 1.7;
  color: var(--ink-2);
  white-space: pre-wrap;
  word-break: break-all;
  user-select: text;
}
.foot .cue { display: inline-flex; align-items: center; gap: 7px; }
.foot .cue i {
  width: 14px; height: 3px; border-radius: 2px; display: inline-block;
  background: hsl(var(--fh, 200) 42% 50% / .95);
}
.foot .cue b { color: var(--ink-2); font-weight: 500; }
.foot .cue .k {
  font-family: var(--font-mo); font-size: 10.5px; color: var(--cheong);
  border: 1px solid var(--cheong); border-radius: 4px; padding: 0 4px;
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

@media (max-width: 560px) {
  .app { padding: 14px; padding-block: 14px 28px; gap: 14px; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .stage { padding: 20px 14px; min-height: 230px; }
  .board { padding: 9px 7px; }
  .foot .keys-help { display: none; }
}
