:root {
  --ink: #1c1612;
  --ink-2: #2a221c;
  --paper: #f3eadc;
  --paper-dim: #d9cbb6;
  --muted: #a89884;
  --seal: #8f2d1f;
  --seal-2: #b6402c;
  --line: rgba(243, 234, 220, 0.14);
  --wood-edge: #5a371c;
  color-scheme: dark;
}

* { box-sizing: border-box; }

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

body {
  background-color: var(--ink);
  background-image:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(224, 168, 92, 0.18), transparent 60%),
    radial-gradient(ellipse 40% 30% at 100% 100%, rgba(120, 36, 22, 0.28), transparent 70%),
    linear-gradient(180deg, #241c17 0%, #14110e 100%);
  color: var(--paper);
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, "Apple Myungjo", "Nanum Myeongjo", "Noto Serif CJK KR", Batang, serif;
  word-break: keep-all;
}

button, select {
  font-family: inherit;
}

.app {
  /* 100vh는 iframe 높이를 다시 재는 순환이 될 수 있다. 뷰포트 비율만 쓴다. */
  height: 100%;
  max-width: 1180px;
  margin: 0 auto;
  padding: 8px 12px 10px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(232px, 292px);
  grid-template-rows: auto minmax(0, 1fr);
  gap: 8px 14px;
  align-items: stretch;
  min-width: 0;
}

.top {
  grid-column: 1 / -1;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  padding-bottom: 6px;
  min-width: 0;
}

.eyebrow {
  margin: 0 0 4px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  font-size: 12px;
  color: var(--muted);
}

h1 {
  margin: 0;
  font-size: clamp(22px, 4.2vh, 44px);
  font-weight: 600;
  letter-spacing: -0.04em;
  line-height: 1;
}

.lede {
  margin: 0;
  max-width: 36em;
  color: var(--paper-dim);
  font-size: 13px;
  line-height: 1.35;
  text-align: right;
}

.top-side {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
  min-width: 0;
  max-width: 40em;
}

#lang {
  width: max-content;
  min-width: 9.5em;
}

.stage {
  min-width: 0;
  min-height: 0;
  container-type: size;
  display: grid;
  place-items: center;
}

.board-frame {
  position: relative;
  width: min(100cqw, 100cqh, 720px);
  max-width: 100%;
  aspect-ratio: 1;
  height: auto;
  filter: drop-shadow(0 8px 12px rgba(0, 0, 0, 0.28));
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 6px;
  touch-action: none;
  cursor: crosshair;
}

.result {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  min-width: 180px;
  max-width: calc(100% - 24px);
  padding: 12px 18px 10px;
  text-align: center;
  background: rgba(28, 22, 18, 0.9);
  border: 1px solid rgba(243, 234, 220, 0.3);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.result strong {
  display: block;
  font-size: 32px;
  font-weight: 600;
  letter-spacing: -0.03em;
  overflow-wrap: anywhere;
}

.result span {
  display: block;
  margin-top: 4px;
  color: var(--paper-dim);
  font-size: 14px;
}

.result.victory {
  bottom: 16%;
  min-width: 220px;
  padding: 18px 28px 14px;
  border-color: rgba(243, 212, 138, 0.9);
  background: rgba(28, 22, 18, 0.9);
  animation: win-pop 0.55s cubic-bezier(0.2, 1.35, 0.4, 1) both;
  box-shadow:
    0 0 0 1px rgba(243, 212, 138, 0.35),
    0 18px 50px rgba(0, 0, 0, 0.45),
    0 0 36px rgba(226, 90, 40, 0.4);
}

.result.victory strong {
  font-size: 48px;
  color: #f3d48a;
}

.result.victory::after {
  content: "";
  position: absolute;
  inset: -10px;
  border: 2px solid rgba(243, 212, 138, 0.85);
  animation: win-ring 0.9s ease-out 2;
  pointer-events: none;
}

@keyframes win-pop {
  0% { opacity: 0; transform: translateX(-50%) scale(0.55); }
  100% { opacity: 1; transform: translateX(-50%) scale(1); }
}

@keyframes win-ring {
  0% { opacity: 0.95; transform: scale(0.86); }
  100% { opacity: 0; transform: scale(1.22); }
}

.panel {
  position: relative;
  background: linear-gradient(180deg, rgba(42, 34, 28, 0.92), rgba(28, 22, 18, 0.92));
  border: 1px solid var(--line);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  height: 100%;
  min-width: 0;
}

.status-row,
.score,
.fields,
.actions,
.keys,
.bgm-credit {
  flex: 0 0 auto;
}

.status-row {
  display: flex;
  gap: 12px;
  align-items: center;
}

.stone-pip {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex: none;
  box-shadow: inset 0 -4px 6px rgba(0, 0, 0, 0.35), 0 2px 4px rgba(0, 0, 0, 0.35);
}

.stone-pip.black {
  background: radial-gradient(circle at 35% 30%, #6d6d6d, #111 62%);
}

.stone-pip.white {
  background: radial-gradient(circle at 35% 30%, #fff, #d9d0c3 70%);
}

.stone-pip.draw {
  background: linear-gradient(135deg, #111 50%, #f3eadc 50%);
}

.status-kicker {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 0.08em;
}

#status {
  margin: 2px 0 0;
  font-size: 16px;
  line-height: 1.3;
}

#status.warn { color: #e7b2a4; }

.score {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin: 0;
  padding: 0;
}

.score div {
  background: rgba(0, 0, 0, 0.22);
  padding: 8px 8px 7px;
  text-align: center;
}

.score dt {
  color: var(--muted);
  font-size: 12px;
}

.score dd {
  margin: 2px 0 0;
  font-size: 18px;
}

#history-toggle {
  width: 100%;
  flex: 0 0 auto;
}

.history-panel {
  position: absolute;
  z-index: 4;
  left: 6px;
  right: 6px;
  bottom: 6px;
  max-height: calc(100% - 7.5rem);
  overflow: auto;
  border: 1px solid rgba(243, 234, 220, 0.2);
  padding: 8px 10px;
  background: rgba(20, 15, 12, 0.97);
  box-shadow: 0 -10px 24px rgba(0, 0, 0, 0.35);
}

.history-panel[hidden] { display: none; }

.history-empty {
  margin: 0;
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.4;
}

#history-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

#history-list li {
  padding: 6px 0;
  border-bottom: 1px solid rgba(243, 234, 220, 0.08);
}

#history-list li:last-child { border-bottom: 0; }

#history-list strong {
  display: block;
  font-size: 13px;
  font-weight: 650;
}

#history-list span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 10px;
}

.fields label, .sound {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.04em;
}

.fields label.wide { grid-column: 1 / -1; }

select {
  appearance: none;
  width: 100%;
  min-width: 0;
  background: #140f0c url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath fill='%23a89884' d='M0 0l6 8 6-8z'/%3E%3C/svg%3E") right 8px center no-repeat;
  color: var(--paper);
  border: 1px solid rgba(243, 234, 220, 0.2);
  padding: 5px 22px 5px 6px;
  font-size: 13px;
}

select:disabled { opacity: 0.45; }

.sound {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--paper-dim);
  letter-spacing: 0;
}

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

#bgm-toggle {
  grid-column: 1 / -1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#bgm { display: none; }

.bgm-credit {
  margin: 0;
  min-width: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.bgm-credit a {
  color: var(--paper-dim);
  overflow-wrap: anywhere;
  word-break: break-word;
}

.bgm-credit a:focus-visible {
  outline: 2px solid #e0a84e;
  outline-offset: 2px;
}

button {
  background: transparent;
  color: var(--paper);
  border: 1px solid rgba(243, 234, 220, 0.28);
  padding: 7px 8px;
  font-size: 14px;
  cursor: pointer;
}

button:hover { border-color: var(--paper); }

button:disabled {
  opacity: 0.4;
  cursor: default;
}

button.primary {
  background: var(--seal);
  border-color: var(--seal);
}

button.primary:hover { background: var(--seal-2); }

button:focus-visible, select:focus-visible {
  outline: 2px solid #e0a84e;
  outline-offset: 2px;
}

.rule-note {
  flex: 0 1 auto;
  margin: 0;
  max-height: 3.2em;
  overflow: auto;
  color: var(--paper-dim);
  font-size: 12.5px;
  line-height: 1.4;
}

.log-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.log-wrap h2 {
  margin: 0 0 6px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.12em;
  color: var(--muted);
}

#log {
  list-style: none;
  margin: 0;
  padding: 0;
  min-height: 0;
  flex: 1 1 auto;
  overflow: auto;
  font-variant-numeric: tabular-nums;
}

#log li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 0;
  border-bottom: 1px solid rgba(243, 234, 220, 0.08);
  font-size: 14px;
}

#log .who { color: var(--muted); }

.keys {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.board-frame.shake {
  animation: shake 0.28s linear;
}

@keyframes shake {
  20% { transform: translateX(-3px); }
  40% { transform: translateX(3px); }
  60% { transform: translateX(-2px); }
  80% { transform: translateX(2px); }
}

@media (max-width: 700px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    padding: 6px 8px 8px;
    gap: 6px;
  }

  .top {
    flex-direction: row;
    align-items: center;
  }

  .top-side {
    flex-direction: row;
    align-items: center;
    width: auto;
    max-width: none;
  }

  .lede { display: none; }

  .eyebrow { display: none; }

  h1 { font-size: 22px; }

  .panel {
    height: auto;
    max-height: none;
  }

  #log { max-height: 72px; flex: 0 1 auto; }
}

@media (max-width: 700px) and (max-height: 680px) {
  .log-wrap,
  .keys,
  .rule-note,
  .status-kicker { display: none; }

  .panel { gap: 4px; padding: 4px 6px 6px; }

  .status-row { gap: 8px; }

  .stone-pip { width: 18px; height: 18px; }

  #status { font-size: 13px; margin: 0; }

  .score { gap: 4px; }

  .score div { padding: 2px 3px 1px; }

  .score dt { font-size: 10px; }

  .score dd { margin: 0; font-size: 14px; }

  .history-panel { max-height: calc(100% - 6.2rem); }

  .fields { gap: 3px 6px; }

  .fields label { font-size: 10px; line-height: 1.15; gap: 1px; }

  select { padding: 2px 16px 2px 4px; font-size: 12px; }

  .actions { gap: 4px; }

  button { padding: 5px 4px; font-size: 12px; line-height: 1.2; }
}

@media (prefers-reduced-motion: reduce) {
  .board-frame.shake { animation: none; }
  .result.victory,
  .result.victory::after { animation: none; }
}
