:root {
  --ink: #16130f;
  --paper: #f4ecdf;
  --paper-dim: #d9cbb6;
  --muted: #a89884;
  --seal: #8f2d1f;
  --seal-2: #b6402c;
  --line: rgba(244, 236, 223, 0.14);
  color-scheme: dark;
}

* { box-sizing: border-box; }

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

body {
  background-color: var(--ink);
  background-image:
    radial-gradient(ellipse 70% 42% at 50% -8%, rgba(196, 148, 78, 0.16), transparent 58%),
    linear-gradient(180deg, #221c16 0%, #100e0c 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 {
  max-width: 1080px;
  margin: 0 auto;
  padding: 14px 16px 20px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 14px 18px;
  align-items: start;
}

.top {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 12px 16px;
  align-items: end;
  border-bottom: 1px solid var(--line);
  padding-bottom: 10px;
}

.brand { min-width: 0; flex: 0 0 auto; }

.top-side {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  min-width: 0;
  max-width: 100%;
  margin-left: auto;
}

.lang-field {
  width: 9.5rem;
  flex: none;
  margin: 0;
}

.eyebrow {
  margin: 0 0 2px;
  letter-spacing: 0.18em;
  font-size: 12px;
  color: var(--muted);
}

h1 {
  margin: 0;
  font-size: 44px;
  font-weight: 600;
  letter-spacing: -0.04em;
  line-height: 0.95;
}

.lede {
  margin: 0;
  color: var(--paper-dim);
  font-size: 16px;
  line-height: 1.45;
  text-align: right;
  max-width: 34rem;
}

.stage { min-width: 0; }

.board-frame {
  position: relative;
  width: min(100%, 680px);
  margin: 0 auto;
  aspect-ratio: 1;
  container-type: inline-size;
  filter: drop-shadow(0 16px 26px rgba(0, 0, 0, 0.35));
}

.board-result {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 2;
  transform: translate(-50%, -50%);
  box-sizing: border-box;
  max-width: 86%;
  margin: 0;
  padding: 0.08em 0.34em 0.12em;
  border: 2px solid #f3d48a;
  border-radius: 12px;
  background: rgba(28, 18, 10, 0.84);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.38);
  color: #f3d48a;
  font-size: 42px;
  font-weight: 750;
  letter-spacing: 0.08em;
  line-height: 1.15;
  text-align: center;
  pointer-events: none;
  user-select: none;
  animation: result-in 0.22s ease-out;
}

@supports (font-size: 1cqi) {
  .board-result { font-size: clamp(18px, 13cqi, 64px); }
}

.board-result[hidden] { display: none; }

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

.panel {
  position: relative;
  background: linear-gradient(180deg, rgba(42, 34, 28, 0.94), rgba(24, 19, 16, 0.94));
  border: 1px solid var(--line);
  padding: 14px 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}

.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%);
}

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

#status {
  margin: 2px 0 0;
  font-size: 18px;
  line-height: 1.35;
}

#status.warn { color: #e7b2a4; }

#status.is-result {
  font-size: 28px;
  font-weight: 750;
  letter-spacing: 0.04em;
  color: #f3d48a;
}

#turn-label.is-result {
  color: #f3d48a;
  font-size: 22px;
}

.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 6px 7px;
  text-align: center;
}

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

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

.score-line {
  margin: 0;
  padding: 8px 10px;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(243, 212, 138, 0.45);
  font-size: 16px;
  line-height: 1.4;
  white-space: pre-line;
}

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

.sheet-toggles button {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sheet {
  position: absolute;
  z-index: 5;
  left: 6px;
  right: 6px;
  top: 9.6rem;
  bottom: 6px;
  overflow: auto;
  padding: 8px 10px 10px;
  background: #1a1512;
  border: 1px solid rgba(244, 236, 223, 0.22);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.4);
}

.sheet[hidden] { display: none; }

.sheet-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  position: sticky;
  top: 0;
  z-index: 1;
  padding-bottom: 6px;
  background: #1a1512;
}

.sheet-bar p {
  margin: 0;
  flex: 1 1 auto;
  min-width: 0;
  font-size: 14px;
  font-weight: 650;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.sheet-bar button {
  width: auto;
  flex: 0 0 auto;
  padding: 4px 8px;
  font-size: 12px;
}

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

.record div {
  background: rgba(0, 0, 0, 0.28);
  padding: 4px 2px 3px;
  text-align: center;
}

.record dt {
  color: var(--muted);
  font-size: 11px;
}

.record dd {
  margin: 0;
  font-size: 16px;
  font-variant-numeric: tabular-nums;
}

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

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

.sheet .row {
  display: block;
  width: 100%;
  margin: 0;
  padding: 7px 2px;
  text-align: left;
  background: transparent;
  border: 0;
  border-bottom: 1px solid rgba(244, 236, 223, 0.1);
  border-radius: 0;
  font-size: 13px;
}

.sheet .row strong {
  display: block;
  font-size: 13px;
  font-weight: 650;
}

.sheet .row span {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.move-line {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 3px 0;
  border-bottom: 1px solid rgba(244, 236, 223, 0.08);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

button.move-line {
  width: 100%;
  margin: 0;
  padding: 4px 2px;
  text-align: left;
  background: transparent;
  border: 0;
  border-bottom: 1px solid rgba(244, 236, 223, 0.08);
  border-radius: 0;
  color: inherit;
  font: inherit;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

button.move-line.is-on {
  background: rgba(243, 212, 138, 0.16);
}

.review-bar {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  gap: 6px;
  margin-bottom: 6px;
}

.review-bar[hidden] { display: none; }

.review-note {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.review-note[hidden] { display: none; }

.move-line .who { color: var(--muted); }

.estimate-line {
  margin: 0;
  padding: 6px 8px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid rgba(243, 212, 138, 0.4);
  font-size: 14px;
  line-height: 1.35;
}

#estimate.is-on {
  border-color: #e0a84e;
  background: rgba(224, 168, 78, 0.16);
}

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

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

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

select {
  appearance: none;
  width: 100%;
  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(244, 236, 223, 0.2);
  padding: 8px 26px 8px 8px;
  font-size: 15px;
}

select:disabled { opacity: 0.45; }

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

#bgm-toggle { grid-column: 1 / -1; }

#bgm-toggle {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#bgm { display: none; }

.bgm-credit {
  margin: 0;
  flex: 0 0 auto;
  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(244, 236, 223, 0.28);
  padding: 9px 10px;
  font-size: 15px;
  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 {
  margin: 0;
  color: var(--paper-dim);
  font-size: 14px;
  line-height: 1.5;
}

.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;
  max-height: 148px;
  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(244, 236, 223, 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: 860px) {
  .app {
    grid-template-columns: 1fr;
    padding: 10px 12px 20px;
    gap: 10px;
  }

  .top {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    padding-bottom: 8px;
  }

  .top-side {
    align-items: flex-start;
    width: 100%;
    margin-left: 0;
  }

  h1 { font-size: 36px; }

  .lede {
    text-align: left;
    font-size: 15px;
    max-width: none;
  }

  .board-frame { width: min(100%, 680px); }

  #log { max-height: 96px; }
}

/* 짧은 화면은 vh 없이 px로 제한한다. iframe 높이가 문서 높이에 묶이면 vh가 순환한다. */
@media (max-height: 720px) and (min-width: 861px) {
  h1 { font-size: 36px; }

  .app {
    padding: 6px 16px 6px;
    gap: 6px 18px;
  }

  .board-frame { width: min(100%, 540px); }

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

  #status { font-size: 15px; line-height: 1.25; }

  #status.is-result { font-size: 22px; }

  .board-result { font-size: 48px; padding: 0.06em 0.28em 0.1em; }

  .score div { padding: 4px 4px 3px; }

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

  .score-line { padding: 4px 8px; font-size: 14px; line-height: 1.3; }

  .fields { gap: 4px 8px; }

  select { padding: 4px 22px 4px 6px; font-size: 14px; }

  .actions { gap: 4px; }

  .sheet { top: 7.2rem; }

  .sheet-toggles { gap: 4px; }

  button { padding: 6px 8px; font-size: 14px; }

  .estimate-line { padding: 3px 8px; font-size: 13px; line-height: 1.3; }

  .rule-note { font-size: 12px; line-height: 1.3; }

  .log-wrap h2 { margin-bottom: 2px; }

  #log { max-height: 32px; }

  .keys { font-size: 11px; }
}

@media (max-width: 860px) and (max-height: 720px) {
  .app {
    padding: 4px 8px 6px;
    gap: 4px;
  }

  .top {
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    gap: 8px;
    padding-bottom: 4px;
  }

  .brand { flex: 1 1 auto; }

  .top-side {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    width: auto;
    max-width: 100%;
    margin-left: 0;
    gap: 2px 8px;
  }

  h1 { font-size: 24px; line-height: 1; }

  .eyebrow {
    font-size: 10px;
    letter-spacing: 0.12em;
    margin-bottom: 0;
  }

  .lede { display: none; }

  .lang-field { width: 7.4rem; }

  #lang { padding: 3px 18px 3px 6px; font-size: 13px; }

  .board-frame { width: min(100%, 200px); }

  .panel { padding: 6px 8px; gap: 2px; }

  .status-row { gap: 8px; }

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

  .status-kicker { font-size: 10px; }

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

  #status.is-result { font-size: 18px; }

  .board-result {
    font-size: 22px;
    padding: 1px 6px 2px;
    border-width: 1px;
    letter-spacing: 0.04em;
  }

  .score { gap: 4px; }

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

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

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

  .fields { gap: 3px 6px; }

  .fields label { gap: 1px; font-size: 10px; letter-spacing: 0; }

  select { padding: 3px 18px 3px 6px; font-size: 13px; }

  .actions { gap: 4px; }

  button { padding: 3px 6px; font-size: 12px; line-height: 1.2; }

  .estimate-line { padding: 2px 6px; font-size: 11px; line-height: 1.2; }

  .score-line { padding: 2px 6px; font-size: 12px; line-height: 1.25; }

  .rule-note { font-size: 11px; line-height: 1.2; }

  .log-wrap,
  .keys { display: none; }

  .sheet { top: 6.2rem; }

  .sheet-toggles { gap: 4px; }

  .log-wrap h2 { font-size: 10px; margin-bottom: 0; }

  #log {
    height: 16px;
    max-height: 16px;
  }

  #log li { padding: 0; font-size: 11px; }

  .keys { font-size: 10px; line-height: 1.15; }

  select { padding: 2px 16px 2px 6px; font-size: 12px; line-height: 1.2; }
}

@keyframes result-in {
  from { transform: translate(-50%, -42%) scale(0.9); opacity: 0; }
  to { transform: translate(-50%, -50%) scale(1); opacity: 1; }
}

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