:root {
  --ink: #14211c;
  --paper: #f4efe4;
  --paper-dim: #d5cbb8;
  --muted: #b4a894;
  --line: rgba(244, 239, 228, 0.16);
  --seal: #8d2f2f;
  --seal-2: #a8433c;
  color-scheme: dark;
}

* { box-sizing: border-box; }

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

body {
  background-color: var(--ink);
  background-image:
    radial-gradient(ellipse 70% 42% at 50% 0%, rgba(232, 208, 150, 0.16), transparent 58%),
    linear-gradient(180deg, #1b2a24 0%, #101614 100%);
  color: var(--paper);
  font-family: "Noto Sans CJK KR", "Noto Sans CJK", "DejaVu Sans", sans-serif;
  word-break: keep-all;
}

button, select { font-family: inherit; }

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

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

.brand { min-width: 0; }

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

h1 {
  margin: 0;
  font-size: 28px;
  font-weight: 650;
  letter-spacing: -0.04em;
  line-height: 1.1;
}

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

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

.board-frame {
  width: min(100cqw, 100cqh, 720px);
  max-width: 100%;
  aspect-ratio: 1;
  height: auto;
  position: relative;
  background: #4a3018;
  user-select: none;
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: pointer;
}

.result {
  position: absolute;
  left: 50%;
  bottom: 8px;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 24px);
  padding: 8px 12px;
  text-align: center;
  background: rgba(20, 16, 12, 0.92);
  border: 1px solid rgba(244, 239, 228, 0.35);
  pointer-events: none;
  z-index: 2;
  animation: win-pop 0.35s ease-out both;
}

.result strong {
  display: block;
  font-size: 20px;
  font-weight: 650;
  line-height: 1.25;
  letter-spacing: -0.03em;
  overflow-wrap: anywhere;
}

.result span {
  display: block;
  margin-top: 2px;
  color: var(--paper-dim);
  font-size: 13px;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

@keyframes win-pop {
  from { opacity: 0; }
  to { opacity: 1; }
}

.promo {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-items: center;
  background: rgba(16, 14, 10, 0.42);
}

.promo[hidden],
.result[hidden] { display: none; }

.promo-box {
  width: min(220px, calc(100% - 24px));
  display: grid;
  gap: 6px;
  padding: 10px;
  background: #2a2118;
  border: 1px solid rgba(244, 239, 228, 0.32);
}

.promo-title {
  margin: 0 0 2px;
  text-align: center;
  font-size: 14px;
  letter-spacing: 0.08em;
}

.panel {
  position: relative;
  grid-column: 2;
  grid-row: 2;
  min-width: 0;
  min-height: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 12px;
  background: linear-gradient(180deg, rgba(31, 48, 40, 0.94), rgba(18, 28, 24, 0.94));
  border: 1px solid var(--line);
  overflow: hidden;
}

.status-row,
.score,
.sheet-toggles,
.fields,
.actions,
.rule-note,
.bgm-credit { flex: 0 0 auto; }

.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;
  overflow-wrap: anywhere;
}

#status.warn { color: #f0b4a8; }

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

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

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

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

.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: 7.4rem;
  bottom: 6px;
  overflow: auto;
  padding: 8px 10px 10px;
  background: #101614;
  border: 1px solid rgba(244, 239, 228, 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: #101614;
}

.sheet-bar p {
  margin: 0;
  flex: 1 1 auto;
  min-width: 0;
  font-size: 13px;
  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;
}

.sheet-empty {
  margin: 0;
  color: var(--muted);
  font-size: 12.5px;
  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, 239, 228, 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: grid;
  grid-template-columns: 2.2em 1fr 1fr;
  gap: 6px;
  padding: 3px 0;
  border-bottom: 1px solid rgba(244, 239, 228, 0.08);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

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

.fields label {
  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, button {
  width: 100%;
  min-width: 0;
}

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

button {
  background: transparent;
  color: var(--paper);
  border: 1px solid rgba(244, 239, 228, 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 #e0b15a;
  outline-offset: 2px;
}

.rule-note {
  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;
  flex: 0 0 auto;
  font-size: 13px;
  font-weight: 650;
  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: 8px;
  min-width: 0;
  padding: 3px 0;
  border-bottom: 1px solid rgba(244, 239, 228, 0.08);
  font-size: 14px;
}

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

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

#bgm-toggle {
  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 #e0b15a;
  outline-offset: 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;
  }

  .stage,
  .panel { grid-column: 1; }

  .panel {
    grid-row: 3;
    height: auto;
    overflow: visible;
  }

  .lede,
  .eyebrow { display: none; }

  h1 { font-size: 22px; }

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

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

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

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

  .score { gap: 4px; }

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

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

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

  .sheet { top: 5.5rem; }

  .sheet-toggles { gap: 4px; }

  .actions { gap: 4px; }

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

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