:root {
  --felt: #1d5c45;
  --felt-dark: #154633;
  --paper: #f7f3ea;
  --ink: #1f2a37;
  --muted: #6b7280;
  --navy: #1f3a5f;
  --red: #c0392b;
  --gold: #e0a526;
  --line: #ddd5c4;
  --radius: 12px;
  font-family: 'Pretendard', 'Apple SD Gothic Neo', 'Malgun Gothic', system-ui, sans-serif;
  color: var(--ink);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at 50% 0%, #26775a, var(--felt) 45%, var(--felt-dark));
}

.top {
  text-align: center;
  color: var(--paper);
  padding: 20px 16px 8px;
  position: relative;
}
.mute-btn {
  position: absolute;
  top: 14px;
  right: 16px;
  font: inherit;
  font-size: 0.85rem;
  color: var(--paper);
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(247, 243, 234, 0.35);
  border-radius: 999px;
  padding: 5px 12px;
  cursor: pointer;
}
.mute-btn:hover {
  background: rgba(0, 0, 0, 0.35);
}
@media (max-width: 480px) {
  .top {
    padding-top: 44px;
  }
}
.btn.resume {
  background: var(--gold);
  border-color: var(--gold);
  color: var(--ink);
}
.top h1 {
  margin: 0;
  font-size: 2rem;
  letter-spacing: 0.08em;
}
.sub {
  margin: 4px 0 0;
  opacity: 0.8;
  font-size: 0.9rem;
}

#app {
  max-width: 980px;
  margin: 0 auto;
  padding: 12px 16px 40px;
}

.panel {
  background: var(--paper);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
}

.setup {
  max-width: 560px;
  margin: 16px auto 24px;
  display: grid;
  gap: 16px;
  padding: 14px 24px 24px;
}
.hero {
  display: block;
  width: calc(100% + 20px);
  height: auto;
  margin: 0 -10px 4px;
  border-radius: 16px;
  filter: drop-shadow(0 6px 14px rgba(0, 0, 0, 0.25));
}
@media (max-width: 480px) {
  .setup {
    padding: 10px 16px 20px;
  }
  .hero {
    width: calc(100% + 12px);
    margin: 0 -6px 2px;
  }
}
.field {
  display: grid;
  gap: 6px;
  font-weight: 600;
}
.field input {
  font: inherit;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.levels {
  border: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 6px;
}
.levels legend {
  font-weight: 600;
  padding: 0;
  margin-bottom: 6px;
}
.level-options {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: #ebe3d2;
  border-radius: 10px;
  padding: 4px;
  gap: 4px;
}
.level-options label {
  position: relative;
  cursor: pointer;
}
.level-options input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
  cursor: pointer;
}
.level-options span {
  display: block;
  text-align: center;
  padding: 8px 0;
  border-radius: 7px;
  font-weight: 600;
  color: var(--muted);
  transition: background 0.15s, color 0.15s;
}
.level-options input:checked + span {
  background: #fff;
  color: var(--navy);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
}
.level-options input:focus-visible + span {
  outline: 2px solid var(--navy);
  outline-offset: 2px;
}
.setup-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.setup-actions .btn {
  flex: 1;
}
.muted {
  color: var(--muted);
  margin: 0;
  font-size: 0.9rem;
}
.rules summary {
  cursor: pointer;
  font-weight: 600;
}
.rules ul {
  padding-left: 18px;
  line-height: 1.6;
  font-size: 0.92rem;
}
kbd {
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 5px;
  background: #fff;
  font-size: 0.85em;
}

.btn {
  font: inherit;
  font-weight: 600;
  border: 2px solid var(--navy);
  background: #fff;
  color: var(--navy);
  border-radius: 10px;
  padding: 10px 18px;
  cursor: pointer;
  transition: transform 0.08s, background 0.15s;
}
.btn:hover:not(:disabled) {
  transform: translateY(-1px);
}
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.btn.primary {
  background: var(--navy);
  color: #fff;
}
.btn.big {
  font-size: 1.15rem;
  padding: 12px 36px;
}
.btn.ghost {
  border-color: transparent;
  background: transparent;
  color: var(--muted);
}
.btn.small {
  padding: 6px 10px;
  font-size: 0.85rem;
}

/* 게임 레이아웃 */
.game {
  display: grid;
  grid-template-columns: 1fr 360px;
  gap: 20px;
  align-items: start;
}
@media (max-width: 820px) {
  .game {
    grid-template-columns: 1fr;
  }
}

.table-area {
  display: grid;
  gap: 18px;
  justify-items: center;
  padding: 18px 8px;
  position: sticky;
  top: 8px;
}
@media (max-width: 820px) {
  .table-area {
    position: static;
  }
}

.status {
  color: var(--paper);
  font-size: 1.1rem;
  font-weight: 600;
  text-align: center;
  min-height: 1.6em;
}
.status .who.ai {
  color: #ffd48a;
}

.tray {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  justify-content: center;
  padding: 42px 22px 22px;
  background: rgba(0, 0, 0, 0.18);
  border-radius: 18px;
  min-height: 120px;
  box-shadow: inset 0 3px 10px rgba(0, 0, 0, 0.3);
}

.die {
  /* 빛은 왼쪽 위에서 비친다고 가정한다.
     - 몸체: 상아색 둥근 면 그라디언트 + 아주 고운 잡음(플라스틱 질감)
     - ::before: 둥근 모서리의 하이라이트/그늘(베벨)과 윗면 광택
     - 눈(.pip): 파인 홈처럼 안쪽 그림자 + 아래 가장자리 반사광
     box-shadow는 바닥 그림자만 둔다 (굴림 애니메이션이 이 값을 바꾼다) */
  width: 72px;
  height: 72px;
  border-radius: 16px;
  border: none;
  padding: 10px;
  display: grid;
  grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  position: relative;
  isolation: isolate;
  cursor: pointer;
  background:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='96' height='96'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='1.1' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.42 0 0 0 0 0.33 0 0 0 0 0.2 0 0 0 0.9 -0.25'/%3E%3C/filter%3E%3Crect width='96' height='96' filter='url(%23n)' opacity='0.35'/%3E%3C/svg%3E"),
    radial-gradient(130% 130% at 26% 20%, #fffefa 0%, #f8f1e3 42%, #ebdfc7 78%, #dccbab 100%);
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.28), 0 7px 14px rgba(0, 0, 0, 0.3);
  transition: transform 0.15s, box-shadow 0.15s;
  will-change: transform;
}
.die::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 0;
  border-radius: inherit;
  pointer-events: none;
  background:
    linear-gradient(140deg, rgba(255, 255, 255, 0.7) 0%, rgba(255, 255, 255, 0) 34%),
    linear-gradient(320deg, rgba(110, 80, 40, 0.2) 0%, rgba(110, 80, 40, 0) 42%);
  box-shadow:
    inset 2px 2px 1px rgba(255, 255, 255, 0.95),
    inset 0 -5px 3px -1px rgba(105, 75, 35, 0.2),
    inset -3px -4px 7px rgba(105, 75, 35, 0.3),
    inset 0 0 0 1px rgba(140, 110, 70, 0.22);
}
.pip {
  position: relative;
  z-index: 1;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  place-self: center;
  --pip: var(--navy);
  background: var(--pip);
  background: radial-gradient(
    circle at 62% 68%,
    color-mix(in srgb, var(--pip) 72%, #fff) 0%,
    var(--pip) 48%,
    color-mix(in srgb, var(--pip) 62%, #000) 100%
  );
  box-shadow:
    inset 1px 2px 2px rgba(0, 0, 0, 0.6),
    0 1px 0 rgba(255, 255, 255, 0.9),
    0 -1px 0 rgba(110, 80, 40, 0.22);
}
.die.face-1 .pip,
.die.face-4 .pip {
  --pip: var(--red);
}
.die.face-1 .pip {
  width: 18px;
  height: 18px;
}
@media (max-width: 480px) {
  .tray {
    gap: 8px;
    padding: 32px 12px 28px;
  }
  .die {
    width: 54px;
    height: 54px;
    padding: 7px;
    border-radius: 12px;
  }
  .pip {
    width: 10px;
    height: 10px;
  }
  .die.face-1 .pip {
    width: 14px;
    height: 14px;
  }
}
.die:disabled {
  cursor: default;
}
.die.blank {
  background: rgba(247, 243, 234, 0.2);
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.22);
}
.die.blank::before {
  display: none;
}
.die.held {
  transform: translateY(-8px);
  box-shadow: 0 0 0 4px var(--gold), 0 10px 16px rgba(0, 0, 0, 0.35);
}
.die.held::after {
  content: '고정';
  position: absolute;
  bottom: -26px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--gold);
}

.controls {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-top: 8px;
}
.rolls-left {
  display: flex;
  gap: 6px;
}
.rolls-left span {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--paper);
}
.rolls-left span.used {
  background: rgba(247, 243, 234, 0.25);
}
.hint {
  color: rgba(247, 243, 234, 0.8);
  font-size: 0.9rem;
  margin: 0;
  text-align: center;
  min-height: 1.3em;
}

/* 점수판 */
.sheet-area {
  background: var(--paper);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
  display: grid;
  gap: 8px;
}
.sheet {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.95rem;
}
.sheet th,
.sheet td {
  padding: 7px 8px;
  border-bottom: 1px solid var(--line);
}
.sheet thead th {
  font-size: 0.85rem;
  color: var(--muted);
  text-align: center;
}
.sheet thead th small {
  display: block;
  font-size: 0.7rem;
  font-weight: 600;
  white-space: nowrap;
}
.sheet thead th.active {
  color: var(--navy);
  box-shadow: inset 0 -3px 0 var(--navy);
}
.sheet th[scope='row'] {
  text-align: left;
  font-weight: 600;
}
.sheet th[scope='row'] small {
  display: block;
  font-weight: 400;
  color: var(--muted);
  font-size: 0.72rem;
}
.sheet td {
  text-align: center;
  width: 72px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}
.sheet tr.sum th,
.sheet tr.sum td {
  background: #efe8d9;
  font-size: 0.88rem;
}
.sheet tr.total th,
.sheet tr.total td {
  background: var(--navy);
  color: #fff;
  font-size: 1.05rem;
  border-bottom: none;
}
.cell-btn {
  font: inherit;
  width: 100%;
  border: 1px dashed #b9ad94;
  background: #fff;
  border-radius: 6px;
  padding: 3px 0;
  color: #9a8f78;
  cursor: pointer;
}
.cell-btn.good {
  color: var(--navy);
  border-color: var(--navy);
  border-style: solid;
}
.cell-btn:hover {
  background: var(--navy);
  color: #fff;
}
.preview {
  color: #b3a990;
  font-weight: 500;
}
td.just-scored {
  animation: flash 1.2s ease-out;
}
@keyframes flash {
  0% {
    background: var(--gold);
  }
  100% {
    background: transparent;
  }
}

.result {
  border: none;
  border-radius: var(--radius);
  padding: 28px;
  width: min(380px, 90vw);
  background: var(--paper);
  text-align: center;
}
.result::backdrop {
  background: rgba(0, 0, 0, 0.5);
}
.result h2 {
  margin-top: 0;
}

/* ---------- 링크 ---------- */
.history-link {
  text-align: center;
  text-decoration: none;
  justify-self: center;
}
.sheet-links {
  display: flex;
  justify-content: space-between;
}
.sheet-links a {
  text-decoration: none;
}
.result-links {
  margin-top: 14px;
}
.result-links a {
  color: var(--navy);
  font-weight: 600;
  font-size: 0.92rem;
}

/* ---------- 경기 기록 페이지 ---------- */
.back-link {
  position: absolute;
  top: 14px;
  left: 16px;
  color: var(--paper);
  text-decoration: none;
  font-size: 0.85rem;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(247, 243, 234, 0.35);
  border-radius: 999px;
  padding: 5px 12px;
}
.back-link:hover {
  background: rgba(0, 0, 0, 0.35);
}
.history {
  max-width: 760px;
  display: grid;
  gap: 16px;
}

.summary {
  display: grid;
  grid-template-columns: 1.4fr repeat(4, 1fr);
  gap: 10px;
}
@media (max-width: 720px) {
  .summary {
    grid-template-columns: repeat(2, 1fr);
  }
  .summary .stat:first-child {
    grid-column: 1 / -1;
  }
}
.stat {
  background: var(--paper);
  border-radius: var(--radius);
  padding: 14px 14px 12px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22);
  display: grid;
  gap: 2px;
  align-content: start;
}
.stat-label {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 600;
}
.stat-value {
  font-size: 1.35rem;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}
.stat-sub {
  font-size: 0.72rem;
  color: var(--muted);
}
.winbar {
  display: flex;
  height: 8px;
  border-radius: 999px;
  overflow: hidden;
  margin-top: 6px;
  background: #e6dcc8;
}
.winbar .w {
  background: #2e9d6a;
}
.winbar .d {
  background: #a3a3a3;
}
.winbar .l {
  background: var(--red);
}

.history-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.filter {
  display: inline-flex;
  background: rgba(0, 0, 0, 0.22);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}
.filter button {
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  border: none;
  background: transparent;
  color: rgba(247, 243, 234, 0.8);
  padding: 6px 14px;
  border-radius: 999px;
  cursor: pointer;
}
.filter button[aria-pressed='true'] {
  background: var(--paper);
  color: var(--navy);
}
.history-count {
  color: rgba(247, 243, 234, 0.75);
  font-size: 0.85rem;
}

.records {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}
.record {
  --accent: var(--navy);
  background: var(--paper);
  border-radius: var(--radius);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22);
  border-left: 6px solid var(--accent);
  overflow: hidden;
}
.record.result-win {
  --accent: #2e9d6a;
}
.record.result-lose {
  --accent: var(--red);
}
.record.result-draw {
  --accent: #8c8c8c;
}
.record summary {
  list-style: none;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  cursor: pointer;
}
.record summary::-webkit-details-marker {
  display: none;
}
.record summary:hover {
  background: rgba(31, 58, 95, 0.04);
}
.record summary:focus-visible {
  outline: 2px solid var(--navy);
  outline-offset: -2px;
}
.result-badge {
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  font-size: 0.9rem;
}
.record-info {
  display: grid;
  gap: 3px;
  min-width: 0;
}
.record-opponent {
  font-size: 1rem;
}
.record-when {
  font-size: 0.8rem;
  color: var(--muted);
}
.record-when > * {
  white-space: nowrap;
}
.record-tags {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}
.tag {
  font-size: 0.7rem;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
  background: #e6dcc8;
  color: #5b4b2c;
}
.tag.gold {
  background: var(--gold);
  color: #4a3208;
}
.record-score {
  text-align: right;
  display: grid;
  gap: 2px;
}
.score-line {
  font-variant-numeric: tabular-nums;
  font-size: 1.5rem;
  font-weight: 800;
  white-space: nowrap;
}
.score-line .mine {
  color: var(--accent);
}
.score-line .colon {
  color: var(--muted);
  margin: 0 4px;
  font-weight: 500;
}
.score-line .theirs {
  color: var(--ink);
  opacity: 0.75;
}
.score-line .unit {
  font-size: 0.9rem;
  margin-left: 2px;
  color: var(--muted);
}
.score-names {
  font-size: 0.75rem;
  color: var(--muted);
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  justify-self: end;
}
.record details[open] summary {
  border-bottom: 1px solid var(--line);
}
.record-detail {
  padding: 8px 16px 14px;
}
.record-sheet td {
  width: 90px;
}
.record-sheet td.better {
  color: #1d7a50;
}
.record-sheet tr.total td,
.record-sheet tr.total th {
  font-size: 1rem;
}

.empty {
  text-align: center;
  display: grid;
  gap: 10px;
  justify-items: center;
  padding: 36px 24px;
}
.empty-title {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}
.empty .btn {
  text-decoration: none;
}
.history-footer {
  text-align: center;
}
.history-footer .btn.ghost {
  color: rgba(247, 243, 234, 0.7);
}

@media (max-width: 480px) {
  .record summary {
    gap: 10px;
    padding: 12px;
  }
  .result-badge {
    width: 44px;
    height: 44px;
    font-size: 0.8rem;
  }
  .score-line {
    font-size: 1.2rem;
  }
  .record-detail {
    padding: 6px 8px 12px;
  }
  .back-link {
    top: 10px;
    left: 10px;
  }
  .stat-value {
    font-size: 1.12rem;
  }
  .record-when .sep {
    display: none;
  }
  .record-when > span:last-child {
    display: block;
  }
}
