:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --text: #1f2128;
  --muted: #6b6f7a;
  --border: #e2e1dc;
  --primary: #3b5bdb;
  --primary-text: #ffffff;
  --danger: #e03131;
  --warn: #f08c00;
  --ok: #2f9e44;
  --radius: 14px;
  --kb-bg: #d4d6dc;
  --key-bg: #ffffff;
  --key-func-bg: #abb0ba;
  --key-shadow: #898a8d;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1d24;
    --surface: #262932;
    --text: #eceef3;
    --muted: #9aa0ad;
    --border: #383c48;
    --primary: #5c7cfa;
    --primary-text: #ffffff;
    --danger: #ff6b6b;
    --warn: #fcc419;
    --ok: #51cf66;
    --kb-bg: #2b2b2b;
    --key-bg: #6b6b6b;
    --key-func-bg: #464646;
    --key-shadow: #1a1a1a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif;
  line-height: 1.5;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

button, input, select { font: inherit; color: inherit; }

/* ---------- ヘッダー ---------- */

.appbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: calc(env(safe-area-inset-top) + 48px);
  padding: env(safe-area-inset-top) 16px 0;
  max-width: 560px;
  margin: 0 auto;
}

.appbar-title {
  font-size: 1.05rem;
  margin: 0;
  letter-spacing: 0.05em;
}

main {
  max-width: 560px;
  margin: 0 auto;
  padding: 0 16px calc(env(safe-area-inset-bottom) + 24px);
}

/* ---------- 共通部品 ---------- */

.screen { display: flex; flex-direction: column; gap: 14px; }

.screen-center {
  min-height: calc(100dvh - 120px);
  justify-content: center;
  align-items: center;
  text-align: center;
}

.screen-center > * { margin: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.card-title { font-size: 0.95rem; margin: 0 0 10px; color: var(--muted); }

.muted { color: var(--muted); font-size: 0.9rem; }

.error { color: var(--danger); min-height: 1.5em; margin: 0; font-size: 0.9rem; }

.btn {
  border: none;
  border-radius: 999px;
  padding: 12px 20px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.btn:disabled { opacity: 0.4; cursor: default; }

.btn-primary { background: var(--primary); color: var(--primary-text); }

.btn-ghost {
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--muted);
  width: 100%;
}

.btn-large { width: 100%; padding: 16px 20px; font-size: 1.1rem; max-width: 420px; }

.btn-text {
  background: none;
  border: none;
  color: var(--muted);
  padding: 8px;
  cursor: pointer;
  text-decoration: underline;
}

.btn-icon {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1.4rem;
  width: 40px;
  height: 40px;
  cursor: pointer;
}

.btn-icon:disabled { opacity: 0.25; cursor: default; }

.chip {
  display: inline-block;
  padding: 2px 12px;
  border-radius: 999px;
  background: var(--player-color, var(--muted));
  color: #fff;
  font-weight: 700;
}

/* ---------- 設定 ---------- */

.player-list { list-style: none; margin: 0 0 10px; padding: 0; display: flex; flex-direction: column; gap: 8px; }

.player-row { display: flex; align-items: center; gap: 10px; }

.player-dot { width: 14px; height: 14px; border-radius: 50%; flex: none; }

.player-row input,
.field select {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 16px; /* iOS の自動ズームを防ぐ */
}

.field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
}

.field > span { flex: none; font-size: 0.95rem; }

.field select { flex: 0 1 60%; }

.field-check { justify-content: flex-start; }

.field-check > span { flex: 1; }

.field-check input { width: 20px; height: 20px; flex: none; accent-color: var(--primary); }

/* ---------- 手渡し ---------- */

.handoff-label { font-size: 1.1rem; }

.handoff-name .chip,
.handoff-name.chip {
  font-size: 2rem;
  padding: 6px 24px;
}

.handoff-next { font-size: 1.2rem; margin-bottom: 24px !important; }

#handoff-char { font-size: 1.8rem; font-weight: 800; }

/* ---------- 回答 ---------- */

/* 回答画面は画面の高さぴったりにして、キーボードが必ず画面内に収まるようにする。
   履歴が増えたら履歴の欄だけをスクロールさせる。 */
.screen-turn {
  height: calc(100dvh - 48px - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 8px);
  gap: 8px;
}

main:has(> #screen-turn:not([hidden])) { padding-bottom: calc(env(safe-area-inset-bottom) + 8px); }

.screen-turn .history { flex: 1 1 0; min-height: 0; max-height: none; }

.screen-turn .error:empty { display: none; }

.turn-head { display: flex; justify-content: space-between; align-items: center; }

.timer-text { font-variant-numeric: tabular-nums; color: var(--muted); }

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

.timer-fill {
  height: 100%;
  width: 100%;
  background: var(--ok);
  transform-origin: left;
  transition: background-color 0.3s;
}

.timer-bar[data-level="warn"] .timer-fill { background: var(--warn); }

.timer-bar[data-level="danger"] .timer-fill { background: var(--danger); }

.timer-bar[data-level="danger"] { animation: pulse 0.6s ease-in-out infinite alternate; }

@keyframes pulse { from { opacity: 1; } to { opacity: 0.45; } }

.history {
  list-style: none;
  margin: 0;
  padding: 4px 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  min-height: 60px;
  max-height: 30dvh;
  overflow-y: auto;
}

.history-full { max-height: none; }

.history-item {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 6px 12px;
  border-left: 4px solid var(--player-color);
  background: var(--surface);
  border-radius: 6px;
}

.history-who { font-size: 0.8rem; color: var(--muted); flex: none; min-width: 4.5em; }

.history-word { font-size: 1.1rem; font-weight: 700; word-break: break-all; }

.answer {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.required { display: flex; align-items: baseline; gap: 8px; }

.required-char { font-size: 2.6rem; font-weight: 800; line-height: 1; color: var(--primary); }

.answer-display {
  min-height: 52px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 2px solid var(--border);
  background: var(--bg);
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  word-break: break-all;
  display: flex;
  align-items: center;
}

/* カーソルの点滅 */
.answer-display::after {
  content: "";
  width: 2px;
  height: 1.2em;
  margin-left: 2px;
  background: var(--primary);
  animation: blink 1s steps(1) infinite;
}

.answer-display.is-empty::before {
  content: "下のキーで入力";
  color: var(--muted);
  font-size: 1rem;
  font-weight: 400;
  letter-spacing: 0;
}

@keyframes blink { 50% { opacity: 0; } }

.btn-giveup { margin-left: auto; font-size: 0.85rem; }

/* ---------- アプリ内キーボード(フリック) ---------- */

.keyboard {
  position: relative;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 50px);
  grid-template-areas:
    "a   ka  sa  del"
    "ta  na  ha  clr"
    "ma  ya  ra  ent"
    "var wa  bar ent";
  gap: 6px;
  padding: 6px;
  margin: 0 -10px;
  border-radius: 16px;
  background: var(--kb-bg);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none; /* フリック中に画面がスクロールしないように */
}

.key {
  padding: 0;
  border: none;
  border-radius: 10px;
  background: var(--key-bg);
  color: var(--text);
  box-shadow: 0 1px 0 var(--key-shadow);
  font-size: 1.45rem;
  cursor: pointer;
}

.key-char.is-pressed,
.key:active { background: var(--key-func-bg); }

.key-func { background: var(--key-func-bg); font-size: 1.3rem; }

.key-func:active { background: var(--key-bg); }

.key-small { font-size: 0.95rem; }

.key-enter { background: var(--primary); color: var(--primary-text); font-size: 1.05rem; font-weight: 700; }

.key-enter:active { background: var(--primary); opacity: 0.8; }

/* フリックの候補表示(押したキーを中心に十字に出す) */
.flick-guide {
  position: absolute;
  display: grid;
  grid-template-columns: repeat(3, var(--cell-w));
  grid-template-rows: repeat(3, var(--cell-h));
  grid-template-areas:
    ".    up     ."
    "left center right"
    ".    down   .";
  pointer-events: none;
  z-index: 10;
}

.flick-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: -2px;
  background: var(--key-bg);
  color: var(--text);
  font-size: 1.6rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
}

.flick-center { grid-area: center; border-radius: 10px; }
.flick-left { grid-area: left; border-radius: 10px 0 0 10px; }
.flick-right { grid-area: right; border-radius: 0 10px 10px 0; }
.flick-up { grid-area: up; border-radius: 10px 10px 0 0; }
.flick-down { grid-area: down; border-radius: 0 0 10px 10px; }

.flick-cell.is-active { background: var(--primary); color: var(--primary-text); }

@media (max-height: 640px) {
  .keyboard { grid-template-rows: repeat(4, 44px); }
}

/* ---------- 判定 ---------- */

.judge-word { font-size: 2.4rem; font-weight: 800; word-break: break-all; }

.judge-list {
  list-style: none;
  padding: 0;
  margin: 0;
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.judge-toggle {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-radius: 12px;
  border: 2px solid var(--player-color);
  background: var(--surface);
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.judge-toggle[data-vote="ng"] { background: color-mix(in srgb, var(--danger) 15%, var(--surface)); }

.judge-mark { font-size: 1.4rem; }

/* ---------- 脱落・結果 ---------- */

.out-icon { font-size: 3rem; }

.out-reason { font-size: 1.1rem; }

.result-hero { text-align: center; }

.result-hero p { margin: 0 0 6px; }

.stats { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0; }

.stats dt { color: var(--muted); font-size: 0.9rem; }

.stats dd { margin: 0; font-weight: 700; word-break: break-all; }

/* ---------- お題 ---------- */

.notice {
  background: color-mix(in srgb, var(--warn) 18%, var(--surface));
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 0.9rem;
  max-width: 420px;
}

.topic-hint { margin: 0; color: var(--muted); font-size: 0.85rem; }

.history-label { margin-left: auto; color: var(--muted); font-size: 0.8rem; text-align: right; }

.field-help { margin: -4px 0 6px; color: var(--muted); font-size: 0.8rem; }

.missed { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }

.missed-item { border-left: 4px solid var(--player-color); padding-left: 10px; }

.missed-head { margin: 0; font-size: 0.85rem; color: var(--muted); }

.missed-body { margin: 2px 0 0; font-weight: 700; }

/* ---------- 遊び方の切り替え・1人用 ---------- */

.mode-switch { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.mode-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 12px 8px;
  border-radius: var(--radius);
  border: 2px solid var(--border);
  background: var(--surface);
  font-weight: 700;
  font-size: 1.05rem;
  cursor: pointer;
}

.mode-btn small { font-weight: 400; font-size: 0.75rem; color: var(--muted); }

.mode-btn[aria-pressed="true"] {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 12%, var(--surface));
}

.keyboard.is-disabled { opacity: 0.45; pointer-events: none; }

.answer-display.is-thinking { color: var(--muted); font-size: 1.1rem; font-weight: 400; }

.answer-display.is-thinking::after { display: none; }

/* ---------- 詰みの種明かし ---------- */

.out-detail {
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 0.9rem;
  line-height: 1.6;
}

.traps .missed-head { color: var(--muted); }

/* ---------- アプリ名・遊び方・シェア・フッター ---------- */

.appbar-title { display: flex; align-items: center; gap: 8px; margin-right: auto; }

.appbar-icon { width: 26px; height: 26px; }

.help h3, .help-title { margin: 0 0 6px; font-size: 1rem; }

.help-title { font-size: 1.15rem; }

.help p, .help ol { margin: 0 0 6px; font-size: 0.95rem; }

.help ol { padding-left: 1.3em; }

.btn-share { border-style: solid; color: var(--primary); border-color: var(--primary); }

.share-status { text-align: center; margin: -6px 0 0; min-height: 1.2em; }

.share-status:empty { display: none; }

.site-footer {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 6px 18px;
  margin-top: 8px;
  font-size: 0.8rem;
}

.site-footer a { color: var(--muted); }

/* ---------- 字数制限(下限〜上限) ---------- */

/* 選択欄が2つ並ぶので、見出しの下に横いっぱいに置く */
.field-length { flex-wrap: wrap; gap: 6px; }

.length-range { flex: 1 1 100%; display: flex; align-items: center; gap: 8px; min-width: 0; }

.length-range select {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 8px;
  font-size: 16px;
}
