/* 常にライト表示。
   役割ごとの見た目:
   Primary   … クリア帯の「次のステージへ」だけ。緑の塗り。
   Secondary … 戻す・やり直す・ヒント・音・ステージ切替。白地に茶の枠。
   Danger    … 全部消す。白地に赤の枠と赤文字。確認の実行ボタンだけ赤の塗り。 */
:root {
  color-scheme: light;
  --bg: #f5efe4;
  --panel: #fffdf9;
  --line: #d8cdbc;
  --text: #2a241d;
  --text-sub: #6b5f51;
  --primary: #246b53;
  --primary-hover: #1d5944;
  --btn-line: #c5b8a4;
  --danger-line: #d98a82;
  --danger-text: #9e2f26;
  --danger-solid: #b83b31;
  --warn: #9e2f26;
  --hint: #8a6410;
  --ok-bg: #e8f2ec;
  --ok-line: #9cc6b2;
  --board-size: min(520px, calc(100vh - 316px), calc(100vw - 32px));
  --board-size: min(520px, calc(100dvh - 316px), calc(100vw - 32px));
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic UI", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

body {
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

.app {
  width: min(100%, 660px);
  margin: 0 auto;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ヘッダー */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 40px;
}
.title-block { display: flex; align-items: baseline; gap: 12px; min-width: 0; }
.app-header h1 {
  margin: 0;
  font-size: 19px;
  font-weight: 700;
  letter-spacing: 0.04em;
  white-space: nowrap;
}
.stage-meta {
  margin: 0;
  color: var(--text-sub);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ボタン共通 */
.btn {
  height: 36px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid transparent;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.12s, border-color 0.12s;
}
.btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.btn:disabled { opacity: 0.42; cursor: default; }

.btn-primary { background: var(--primary); color: #ffffff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary { background: var(--panel); color: var(--text); border-color: var(--btn-line); }
.btn-secondary:hover:not(:disabled) { background: #f6efe3; }

.btn-danger { background: var(--panel); color: var(--danger-text); border-color: var(--danger-line); }
.btn-danger:hover:not(:disabled) { background: #fbeeec; }

.btn-danger-solid { background: var(--danger-solid); color: #ffffff; border-color: var(--danger-solid); }
.btn-danger-solid:hover { background: #a0322a; }

/* ステージ選択 */
.stage-bar { display: flex; align-items: center; gap: 10px; height: 36px; }
.stage-bar-label { color: var(--text-sub); font-size: 13px; white-space: nowrap; }
.stage-buttons {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 48px));
  gap: 6px;
}
.stage-btn {
  position: relative;
  width: 100%;
  padding: 0;
  font-variant-numeric: tabular-nums;
}
.stage-btn[aria-current="true"] {
  background: #e6f0eb;
  border: 2px solid var(--primary);
  color: var(--primary);
}
.stage-btn.is-cleared::after {
  content: "";
  position: absolute;
  top: 4px;
  right: 5px;
  width: 4px;
  height: 8px;
  border: solid var(--primary);
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

/* ルール 1 行 */
.rule-guide {
  margin: 0;
  min-height: 20px;
  color: var(--text-sub);
  font-size: 13px;
}

/* 盤面 */
.board-wrap { display: flex; justify-content: center; }
.board {
  display: block;
  width: var(--board-size);
  height: var(--board-size);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}
.board .cell { fill: transparent; }
.paper-swirl { fill: none; stroke: #e4dccd; stroke-width: 10; stroke-linecap: round; opacity: 0.7; }
.wood-grain { fill: none; stroke: #b48b52; stroke-width: 1.4; opacity: 0.22; }

.stitch path { fill: none; stroke-linecap: round; }
.stitch--under path { stroke: #fffaf0; stroke-width: 5; }
.stitch path { stroke: #8f867a; stroke-width: 1.8; stroke-dasharray: 7 6; }
.stitch--under path { stroke-dasharray: none; }
.region-edge path { fill: none; stroke: #4a423a; stroke-opacity: 0.6; stroke-width: 2.2; stroke-linecap: round; }
.lead-edge path { fill: none; stroke-width: 5; stroke-linecap: round; }

.wall-bar { stroke: #1f1c19; stroke-width: 11; stroke-linecap: round; }
.wall-shine { stroke: #6f665d; stroke-width: 2; stroke-linecap: round; opacity: 0.85; }
.wall--violated .wall-bar { stroke: #d93829; animation: wall-pulse 1s ease-in-out infinite; }
.wall--violated .wall-shine { stroke: #f4a196; }
.wall-ring {
  fill: rgba(217, 56, 41, 0.1);
  stroke: #d93829;
  stroke-width: 3;
  stroke-dasharray: 9 7;
  animation: ring-spin 6s linear infinite;
  transform-box: fill-box;
  transform-origin: center;
}
.wall--flash .wall-bar { animation: wall-flash 0.6s ease-out; }
@keyframes wall-pulse { 50% { stroke-width: 14; stroke: #ef6a5a; } }
@keyframes wall-flash { 0%, 40% { stroke: #d93829; stroke-width: 15; } 100% { stroke: #1f1c19; stroke-width: 11; } }
@keyframes ring-spin { to { transform: rotate(360deg); } }

.clue-number {
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", "Hiragino Mincho ProN", serif;
  font-size: 50px;
  text-anchor: middle;
  dominant-baseline: central;
  fill: #ffffff;
  stroke: #1b1713;
  stroke-width: 7px;
  stroke-linejoin: round;
  paint-order: stroke fill;
  pointer-events: none;
}
.progress-pill rect { fill: #fffdf9; fill-opacity: 0.94; stroke: #2f2a24; stroke-width: 1.4; }
.progress-pill text {
  font-family: "Hiragino Sans", system-ui, sans-serif;
  font-size: 13px;
  font-weight: 700;
  fill: #2a241d;
  text-anchor: middle;
  dominant-baseline: central;
  font-variant-numeric: tabular-nums;
}

.shimmer { animation: glass-sweep 1.2s ease-out forwards; }
@keyframes glass-sweep { from { transform: translateX(0); } to { transform: translateX(var(--sweep)); } }

.hint-ring {
  fill: none;
  stroke-width: 5;
  stroke-dasharray: 12 8;
  animation: hint-march 0.9s linear infinite;
}
.hint-ring--next { stroke: #d4a017; }
.hint-ring--wrong { stroke: #d93829; }
@keyframes hint-march { to { stroke-dashoffset: -40; } }

/* ステータス帯 */
.status-band {
  display: flex;
  align-items: center;
  gap: 14px;
  height: 52px;
  padding: 0 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.status-counts {
  display: flex;
  gap: 14px;
  white-space: nowrap;
  color: var(--text-sub);
  font-size: 13px;
}
.status-counts strong { color: var(--text); font-size: 16px; font-variant-numeric: tabular-nums; }
.status-message {
  flex: 1;
  margin: 0;
  min-width: 0;
  font-size: 13px;
  line-height: 1.35;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.status-message[data-tone="warn"] { color: var(--warn); font-weight: 600; }
.status-message[data-tone="hint"] { color: var(--hint); font-weight: 600; }
.status-message[data-tone="info"] { color: var(--text-sub); }
.status-band.is-cleared { background: var(--ok-bg); border-color: var(--ok-line); }
.status-band.is-cleared .status-message { color: var(--primary); font-weight: 700; font-size: 15px; }

/* 操作バーと確認行 */
.action-bar { display: flex; align-items: center; gap: 8px; height: 36px; }
.action-spacer { flex: 1; }
.confirm-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  min-height: 36px;
  visibility: hidden;
}
.confirm-row.is-open { visibility: visible; }
.confirm-text { color: var(--danger-text); font-weight: 600; margin-right: 4px; }

/* 狭い画面（スマホ）: ステータス帯を 2 段にし、タップ領域を広げ、1 画面に収める */
@media (max-width: 560px) {
  :root {
    --board-size: min(440px, max(240px, calc(100vh - 392px)), calc(100vw - 24px));
    --board-size: min(440px, max(240px, calc(100dvh - 392px)), calc(100vw - 24px));
  }
  .app { padding: 10px 12px; gap: 8px; }
  .title-block { flex-direction: column; align-items: flex-start; gap: 0; }
  .app-header h1 { font-size: 17px; }
  .stage-meta { font-size: 12px; }
  .stage-bar { height: 38px; }
  .stage-bar-label { display: none; }
  .stage-buttons { grid-template-columns: repeat(8, 1fr); gap: 4px; }
  .stage-btn { height: 38px; }
  .rule-guide { font-size: 12px; line-height: 1.4; }
  .status-band {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    align-items: center;
    column-gap: 10px;
    row-gap: 4px;
    height: auto;
    min-height: 76px;
    padding: 8px 12px;
  }
  .status-counts { grid-column: 1; grid-row: 1; gap: 10px; font-size: 12px; }
  .status-counts strong { font-size: 15px; }
  #btn-next { grid-column: 2; grid-row: 1 / span 2; height: 40px; padding: 0 12px; }
  .status-message {
    grid-column: 1;
    grid-row: 2;
    display: block;
    -webkit-line-clamp: unset;
    overflow: visible;
    font-size: 12.5px;
    line-height: 1.35;
  }
  .status-band.is-cleared .status-message { font-size: 13.5px; }
  .status-band:not(.is-cleared) .status-counts,
  .status-band:not(.is-cleared) .status-message { grid-column: 1 / -1; }
  .action-bar { height: 40px; gap: 6px; }
  .action-bar .btn { height: 40px; padding: 0 11px; font-size: 13px; }
  .confirm-row { min-height: 40px; font-size: 13px; }
  .confirm-row .btn { height: 38px; }
}
