:root {
  --bg: #121726;
  --panel: #212940;
  --panel-light: #303a59;
  --accent: #ffbd33;
  --good: #59d98c;
  --bad: #ff6161;
  --warn: #ffb34d;
  --text: #f2f4ff;
  --dim: #9eaacc;
  --card: #f7f7ee;
  --card-text: #1a1f33;
  --op: #fa9e2e;
  --paren: #40b3c7;
  --radius: 14px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: "Yu Gothic UI", "Hiragino Sans", "Noto Sans JP", "Noto Sans CJK JP", Meiryo, system-ui, sans-serif;
  overscroll-behavior: none;
}

#app {
  max-width: 560px; margin: 0 auto;
  height: 100dvh; min-height: 100vh;
  padding: env(safe-area-inset-top) 14px env(safe-area-inset-bottom);
}

.hidden { display: none !important; }

.screen { height: 100%; display: flex; flex-direction: column; gap: 8px; padding: 10px 0; position: relative; }

button { font: inherit; color: inherit; border: 0; cursor: pointer; }
button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.btn {
  min-height: 52px; padding: 8px 12px; border-radius: var(--radius);
  background: var(--panel); color: var(--text); font-weight: 700; font-size: 1.05rem;
  touch-action: manipulation; user-select: none; -webkit-user-select: none;
  transition: transform .06s, filter .1s;
}
.btn:active { transform: scale(.97); filter: brightness(.85); }
.btn.good { background: var(--good); color: var(--card-text); }
.btn.dim { background: var(--panel-light); }
.btn.ghost { background: var(--panel); color: var(--dim); }
.btn.ghost.warn { color: var(--warn); }
.btn.big { min-height: 64px; font-size: 1.6rem; margin-top: auto; }
.btn[disabled] { opacity: .4; pointer-events: none; }

/* ---------- 開始画面 ---------- */
h1 { margin: 6px 0 0; text-align: center; color: var(--accent); font-size: 2rem; }
.sub { margin: 0 0 4px; text-align: center; color: var(--dim); }
#settings { display: flex; flex-direction: column; gap: 8px; overflow-y: auto; flex: 1 1 auto; min-height: 0; padding-bottom: 4px; }
.field-label { font-size: .85rem; color: var(--dim); margin: 0 4px 3px; }
.seg { display: flex; gap: 6px; }
.seg .btn { flex: 1; font-size: .95rem; min-height: 48px; padding: 6px 4px; }
.seg .btn.on { background: var(--accent); color: var(--card-text); }
.seg.ops .btn { font-size: 1.6rem; }
.seg.ops .btn.on { background: var(--op); }
.field.disabled { opacity: .35; pointer-events: none; }
.preview { margin: 0; text-align: center; color: var(--dim); font-size: .85rem; min-height: 2.6em; }

/* ---------- プレイ画面 ---------- */
.hud { display: flex; align-items: center; justify-content: space-between; font-weight: 800; }
.hud-score { flex: 1; font-size: 1.1rem; }
.hud-time { flex: 1; text-align: center; font-size: 1.5rem; color: var(--accent); }
.hud-time.low { color: var(--bad); }
.hud-time.relaxed { color: var(--dim); font-size: 1.1rem; }
.hud-miss { flex: 1; text-align: right; color: var(--bad); font-size: 1.1rem; }

.target { text-align: center; color: var(--accent); font-size: clamp(2.2rem, 9vw, 3.2rem); font-weight: 900; line-height: 1.1; }
.rule { text-align: center; color: var(--dim); font-size: .78rem; }

.cards {
  display: flex; justify-content: center; gap: 8px; min-height: 96px;
}
.card {
  flex: 0 1 100px; max-width: 100px; aspect-ratio: 4 / 5; min-height: 92px;
  background: var(--card); color: var(--card-text);
  border-radius: var(--radius); font-size: 2.6rem; font-weight: 900;
  touch-action: none; user-select: none; -webkit-user-select: none;
  box-shadow: 0 3px 0 rgba(0,0,0,.35);
}
.card.used { background: #4c5266; color: #8b93ab; box-shadow: none; }
.card.next { background: #fbe7ae; }
.card.dragging { opacity: .35; }
.card.drop-target { outline: 3px dashed var(--accent); }

.expr {
  display: flex; align-items: center; justify-content: center; flex-wrap: nowrap; gap: 4px;
  background: var(--panel); border-radius: var(--radius); min-height: 92px; padding: 8px 6px;
  overflow: hidden;
}
.expr .hint { color: var(--dim); font-size: .9rem; text-align: center; }
.expr.drop-hover { outline: 3px dashed var(--paren); }
.tok {
  flex: 0 1 56px; min-width: 0; height: 70px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 10px; font-size: 1.9rem; font-weight: 900; color: var(--card-text);
  background: var(--card);
}
.tok.op { background: var(--op); }
.tok.paren { background: var(--paren); cursor: pointer; }
.expr.many .tok { font-size: 1.4rem; height: 58px; }

.msg { min-height: 1.6em; text-align: center; font-weight: 700; font-size: .95rem; }
.msg.good { color: var(--good); } .msg.bad { color: var(--bad); } .msg.warn { color: var(--warn); } .msg.dim { color: var(--dim); }

.ops { display: flex; gap: 8px; }
.ops .btn { flex: 1; min-height: 68px; background: var(--op); color: var(--card-text); font-size: 2.2rem; }

.parens { display: flex; gap: 12px; justify-content: center; }
.btn.paren {
  width: 38%; min-height: 56px; background: var(--paren); color: var(--card-text); font-size: 2rem;
  touch-action: none;
}
.btn.paren.dragging { opacity: .4; }
.paren-help { text-align: center; color: var(--dim); font-size: .72rem; margin-top: -4px; }

.controls { display: flex; gap: 8px; }
.controls .btn { flex: 1; }
.controls #btn-submit { flex: 1.3; font-size: 1.4rem; }
.bottom { display: flex; gap: 8px; margin-top: auto; }
.bottom .btn { flex: 1; min-height: 44px; font-size: .95rem; }

.loading {
  position: absolute; inset: 0; z-index: 5; display: flex; align-items: center; justify-content: center;
  background: rgba(10, 13, 24, .92); font-size: 1.6rem; font-weight: 800;
}
.loading span { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .4; } }

.drag-ghost {
  position: fixed; z-index: 100; pointer-events: none; transform: translate(-50%, -50%);
  min-width: 64px; height: 76px; padding: 0 14px;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius); font-size: 2.4rem; font-weight: 900; opacity: .9;
  background: var(--card); color: var(--card-text); box-shadow: 0 8px 20px rgba(0,0,0,.5);
}
.drag-ghost.paren { background: var(--paren); }

/* ---------- 結果画面 ---------- */
h2 { margin: 6px 0 0; text-align: center; color: var(--accent); font-size: 2rem; }
.summary { text-align: center; line-height: 1.7; }
.summary .big { font-size: 1.8rem; font-weight: 900; }
.summary .small { color: var(--dim); font-size: .85rem; }
.result-list { flex: 1 1 auto; min-height: 0; overflow-y: auto; background: var(--panel); border-radius: var(--radius); padding: 10px; }
.result-list h3 { margin: 10px 0 6px; color: var(--accent); font-size: 1.1rem; }
.result-list h3:first-child { margin-top: 0; }
.result-list .note { color: var(--dim); margin: 4px 2px 8px; font-size: .9rem; }
.entry { background: var(--panel-light); border-left: 5px solid var(--bad); border-radius: 8px; padding: 8px 10px; margin-bottom: 8px; line-height: 1.6; font-size: .95rem; overflow-wrap: anywhere; }
.entry.skip { border-left-color: var(--warn); }
.entry b { color: var(--dim); font-weight: 600; }
.result-buttons { display: flex; gap: 8px; }
.result-buttons .btn { flex: 1; min-height: 58px; font-size: 1.2rem; }

@media (max-height: 700px) {
  .card { min-height: 76px; font-size: 2.1rem; }
  .tok { height: 56px; }
  .expr { min-height: 74px; }
  .ops .btn { min-height: 54px; }
  .paren-help { display: none; }
}
