:root {
  --felt-1: #1b6a4c;
  --felt-2: #0f4733;
  --felt-3: #0a2e22;
  --bar: rgba(5, 24, 17, 0.82);
  --text: #f3eedf;
  --muted: #a9c3b4;
  --gold: #f2c14e;
  --gold-hover: #ffd36b;
  --paper: #fbf8ef;
  --paper-ink: #2a2620;
  --paper-line: #e3d9c2;
  --green: #1f7a55;
  --tile-back: #1f7a55;
  --tile-side: #dcc99b;
  --select: #ffcf3f;
  --hint: #4fd2ff;
  --danger: #ff8a65;
  --serif: 'Noto Serif JP', 'Hiragino Mincho ProN', 'Yu Mincho', 'YuMincho', serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: radial-gradient(120% 90% at 50% 40%, var(--felt-1) 0%, var(--felt-2) 55%, var(--felt-3) 100%) fixed;
  color: var(--text);
  font-family: system-ui, -apple-system, 'Hiragino Sans', 'Noto Sans JP', 'Yu Gothic UI', Meiryo, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button,
input,
select {
  font: inherit;
}

[hidden] {
  display: none !important;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

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

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 20px;
  padding: max(8px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  background: var(--bar);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 2px 14px rgba(0, 0, 0, 0.3);
  position: relative;
  z-index: 10;
}

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

.brand-mark {
  display: grid;
  place-items: center;
  width: 26px;
  height: 34px;
  border-radius: 4px;
  background: linear-gradient(160deg, #fffdf6, #ece2c8);
  box-shadow: 1px 1px 0 var(--tile-side), 3px 3px 0 var(--tile-back);
  color: #c4262e;
  font-family: var(--serif);
  font-weight: 700;
  font-size: 18px;
  line-height: 1;
}

.brand-name {
  margin: 0;
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.12em;
  white-space: nowrap;
}

.stats {
  display: flex;
  gap: 18px;
  margin: 0;
}

.stat {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 52px;
}

.stat dt {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.05em;
}

.stat dd {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.25;
}


.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-left: auto;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 36px;
  padding: 0 13px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 0.15s, transform 0.1s, opacity 0.15s;
}

.btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.16);
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

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

.btn:focus-visible,
.tile:focus-visible,
select:focus-visible,
input:focus-visible {
  outline: 3px solid var(--hint);
  outline-offset: 2px;
}

.btn-primary {
  border-color: transparent;
  background: var(--gold);
  color: #2b2108;
}

.btn-primary:hover:not(:disabled) {
  background: var(--gold-hover);
}

.icon {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.level select {
  height: 36px;
  padding: 0 32px 0 14px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%23f3eedf' stroke-width='1.8' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat right 12px center / 11px 8px;
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  appearance: none;
}

.level select option {
  color: #222;
}

/* ------------------------------------------------------------ 盤面 */

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 14px max(14px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  overflow: hidden;
}

.board {
  position: relative;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.board.is-busy {
  cursor: progress;
}

.tile {
  position: absolute;
  width: var(--tile-w);
  height: var(--tile-h);
  /* % 指定だと盤の幅が基準になるので、牌の大きさから計算する */
  padding: calc(var(--tile-h) * 0.03) calc(var(--tile-w) * 0.05);
  border: 0;
  border-radius: calc(var(--tile-w) * 0.12);
  background: linear-gradient(155deg, #fffef9 0%, #f6f0e0 55%, #ebe1c6 100%);
  box-shadow:
    inset 0 0 0 1px rgba(110, 90, 50, 0.22),
    inset 0 -2px 3px rgba(150, 120, 60, 0.12),
    calc(var(--depth) * 0.5) calc(var(--depth) * 0.5) 0 var(--tile-side),
    var(--depth) var(--depth) 0 var(--tile-back),
    calc(var(--depth) + 1px) calc(var(--depth) + 3px) 6px rgba(0, 0, 0, 0.35);
  cursor: pointer;
  transition: transform 0.12s ease, opacity 0.25s ease, filter 0.15s, visibility 0s;
}

.tile svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}

@media (hover: hover) {
  .tile:hover {
    filter: brightness(1.05);
  }
}

.tile.selected {
  z-index: 2;
  background: linear-gradient(155deg, #fff9d6 0%, #ffeaa0 100%);
  outline: 3px solid var(--select);
  outline-offset: 0;
  transform: translate(-1px, -2px);
}

.tile.hinted {
  z-index: 1;
  background: linear-gradient(155deg, #eefaff 0%, #c9edff 100%);
  outline: 3px solid var(--hint);
  outline-offset: 0;
  animation: hint-pulse 1s ease-in-out infinite;
}

@keyframes hint-pulse {
  50% { outline-color: rgba(79, 210, 255, 0.25); }
}

.tile.gone {
  opacity: 0;
  transform: scale(0.75);
  visibility: hidden;
  pointer-events: none;
  transition: transform 0.25s ease, opacity 0.25s ease, visibility 0s 0.25s;
}

.tile.shake {
  animation: shake 0.3s ease;
}

@keyframes shake {
  25% { translate: -3px 0; }
  75% { translate: 3px 0; }
}

.board.is-dealing .tile:not(.gone) {
  animation: deal 0.38s ease-out backwards;
  animation-delay: var(--deal-delay, 0ms);
}

@keyframes deal {
  from { opacity: 0; translate: 0 -12px; scale: 0.9; }
}

.path-layer {
  position: absolute;
  inset: 0;
  z-index: 5;
  overflow: visible;
  pointer-events: none;
}

.path-line {
  fill: none;
  stroke: #ffd84d;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 0 4px rgba(255, 196, 30, 0.9));
  animation: path-fade 0.38s ease-in forwards;
}

@keyframes path-fade {
  0%, 60% { opacity: 1; }
  100% { opacity: 0; }
}

.penalty-pop {
  position: absolute;
  top: 10px;
  left: 50%;
  z-index: 20;
  padding: 6px 16px;
  border-radius: 999px;
  background: rgba(196, 38, 46, 0.94);
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
  pointer-events: none;
  animation: penalty-pop 1.8s ease-out forwards;
}

@keyframes penalty-pop {
  0% { opacity: 0; transform: translate(-50%, 8px); }
  12% { opacity: 1; transform: translate(-50%, 0); }
  75% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, -12px); }
}

.stage-message {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 14px;
  padding: 24px;
  text-align: center;
  font-size: 16px;
  color: var(--text);
}

.stage-message p {
  margin: 0;
  max-width: 28em;
  line-height: 1.7;
}

/* ------------------------------------------------------------ ダイアログ */

.dialog {
  width: min(420px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: var(--paper);
  color: var(--paper-ink);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.5);
}

.dialog-wide {
  width: min(640px, calc(100vw - 32px));
}

.dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.dialog[open] {
  animation: dialog-in 0.18s ease-out;
}

@keyframes dialog-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
}

.dialog-body {
  padding: 24px;
}

.dialog-body > p {
  line-height: 1.7;
}

.dialog-title {
  margin: 0 0 14px;
  font-family: var(--serif);
  font-size: 21px;
  text-align: center;
  letter-spacing: 0.06em;
}

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 20px;
}

.dialog .btn {
  border-color: var(--paper-line);
  background: #efe8d6;
  color: var(--paper-ink);
}

.dialog .btn:hover:not(:disabled) {
  background: #e6dcc4;
}

.dialog .btn-primary {
  border-color: transparent;
  background: var(--green);
  color: #fff;
}

.dialog .btn-primary:hover:not(:disabled) {
  background: #258f64;
}

.dialog .btn-ghost {
  display: flex;
  margin: 12px auto 0;
  border-color: transparent;
  background: none;
  color: #6b6252;
  font-weight: 500;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.confirm-text {
  margin: 4px 0 0;
  white-space: pre-line;
  line-height: 1.7;
  text-align: center;
}

.result-title {
  color: #b07d10;
  font-size: 30px;
  letter-spacing: 0.25em;
}

.result-time {
  margin: 0;
  font-size: 46px;
  font-weight: 800;
  line-height: 1.1;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.result-detail {
  display: grid;
  gap: 4px;
  margin: 16px auto 0;
  width: fit-content;
  font-size: 14px;
}

.result-detail div {
  display: flex;
  justify-content: space-between;
  gap: 24px;
}

.result-detail dt {
  color: #7a705f;
}

.result-detail dd {
  margin: 0;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.result-rank {
  margin: 16px 0 0;
  font-weight: 700;
  text-align: center;
  color: var(--green);
}

.result-entry {
  margin-top: 12px;
}

.result-entry label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  color: #7a705f;
}

.result-entry-row {
  display: flex;
  gap: 8px;
}

.result-entry input {
  flex: 1;
  min-width: 0;
  height: 40px;
  padding: 0 12px;
  border: 1px solid #cfc3a6;
  border-radius: 10px;
  background: #fff;
  color: var(--paper-ink);
  font-size: 16px;
}

.result-entry .btn {
  height: 40px;
}

.tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  margin-bottom: 12px;
  border-radius: 999px;
  background: #ece4d0;
}

.tab {
  flex: 1;
  height: 32px;
  border: 0;
  border-radius: 999px;
  background: none;
  color: #6b6252;
  font-weight: 600;
  cursor: pointer;
}

.tab[aria-selected='true'] {
  background: #fff;
  color: var(--paper-ink);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

.ranking-wrap {
  max-height: min(60dvh, 520px);
  overflow-y: auto;
  border: 1px solid var(--paper-line);
  border-radius: 10px;
}

.ranking {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

.ranking th {
  position: sticky;
  white-space: nowrap;
  top: 0;
  padding: 8px 10px;
  background: #f1ebdc;
  color: #7a705f;
  font-size: 12px;
  font-weight: 600;
  text-align: left;
}

.ranking td {
  padding: 8px 10px;
  border-top: 1px solid var(--paper-line);
}

.ranking .col-rank {
  width: 3em;
  font-weight: 700;
  text-align: center;
}

.ranking .col-name {
  max-width: 10em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ranking .col-time {
  font-weight: 700;
}

.ranking .col-opt,
.ranking .col-date {
  color: #7a705f;
}

.ranking tbody tr:nth-child(1) .col-rank { color: #c08a00; }
.ranking tbody tr:nth-child(2) .col-rank { color: #8a8f99; }
.ranking tbody tr:nth-child(3) .col-rank { color: #a0643a; }

.ranking tr.is-me td {
  background: #fff3c4;
}

.ranking-empty {
  padding: 28px 12px !important;
  color: #7a705f;
  text-align: center;
}

.help ol,
.help ul {
  margin: 0;
  padding-left: 1.4em;
  line-height: 1.75;
}

.help li + li {
  margin-top: 4px;
}

kbd {
  display: inline-block;
  min-width: 1.6em;
  padding: 0 0.35em;
  border: 1px solid #cfc3a6;
  border-bottom-width: 2px;
  border-radius: 4px;
  background: #fff;
  font: 600 12px/1.5 ui-monospace, monospace;
  text-align: center;
}

.help-notes {
  margin-top: 14px !important;
  padding-top: 12px !important;
  border-top: 1px solid var(--paper-line);
  font-size: 13px;
  color: #5d5547;
}

/* ------------------------------------------------------------ 通知 */

.toast {
  position: fixed;
  inset: auto auto max(20px, env(safe-area-inset-bottom)) 50%;
  z-index: 100;
  max-width: calc(100vw - 32px);
  margin: 0;
  padding: 10px 18px;
  border: 0;
  border-radius: 999px;
  background: rgba(20, 20, 20, 0.88);
  color: #fff;
  font-size: 14px;
  text-align: center;
  opacity: 0;
  transform: translate(-50%, 12px);
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ポップオーバー対応ブラウザ (未対応だとこのルールごと無視される) */
.toast:popover-open {
  opacity: 1;
  transform: translate(-50%, 0);
  animation: toast-in 0.2s ease-out;
}

@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 12px); }
}

/* ------------------------------------------------------------ スマートフォン */

@media (max-width: 720px) {
  .topbar {
    gap: 6px 12px;
    padding-left: max(10px, env(safe-area-inset-left));
    padding-right: max(10px, env(safe-area-inset-right));
  }

  .brand-mark {
    width: 20px;
    height: 27px;
    font-size: 14px;
  }

  .brand-name {
    font-size: 18px;
  }

  .stats {
    gap: 12px;
    margin-left: auto;
  }

  .stat {
    min-width: 44px;
  }

  .stat dd {
    font-size: 17px;
  }

  .controls {
    width: 100%;
    flex-wrap: nowrap;
    gap: 3px;
  }

  .controls .btn {
    flex: 1 1 0;
    flex-direction: column;
    gap: 1px;
    min-width: 0;
    height: 46px;
    padding: 0 1px;
    border-radius: 10px;
    font-size: 10px;
    letter-spacing: -0.03em;
  }

  .controls .btn-help {
    flex: 0.65 1 0;
  }

  .btn-help span {
    display: none;
  }

  .level {
    flex: 1.05 1 0;
    min-width: 0;
  }

  /* 幅が狭いので、ほかのボタンと同じく文字を中央に、▼をその下に置く */
  .level select {
    width: 100%;
    height: 46px;
    padding: 0 2px 13px;
    border-radius: 10px;
    background-position: center bottom 9px;
    background-size: 10px 7px;
    font-size: 12px;
    text-align: center;
    text-align-last: center;
  }

  .stage {
    padding: 8px;
  }

  .dialog-body {
    padding: 20px 16px;
  }

  .ranking th,
  .ranking td {
    padding: 7px 6px;
  }
}

@media (max-width: 480px) {
  .ranking .col-date {
    display: none;
  }
}

/* 幅 320px 前後の小さな画面でも「シャッフル」「ランキング」がボタンに収まるようにする */
@media (max-width: 359px) {
  .controls .btn {
    font-size: 9px;
    letter-spacing: -0.06em;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
