/* ---------------------------------------------------------------- reset */

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

/* `hidden` is only a UA rule, so any author display rule would beat it. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: 'Apple SD Gothic Neo', 'Noto Sans KR', 'Malgun Gothic', system-ui, sans-serif;
  color: var(--ink);
  background: #0b2418;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

button, select { font: inherit; color: inherit; }

/* --------------------------------------------------------------- tokens */

:root {
  --panel: rgba(7, 26, 18, 0.66);
  --panel-solid: #10291e;
  --edge: rgba(233, 198, 103, 0.16);
  --edge-strong: rgba(233, 198, 103, 0.38);
  --ink: #f4ead2;
  --ink-dim: #9fb8a6;
  --gold: #e9c667;
  --gold-deep: #a8801f;
  --red: #d8453a;
  --green: #55bb7c;
  --card-face: #f7edd6;
  --card-w: 60px;
  --radius: 14px;
  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.5), 0 4px 10px rgba(0, 0, 0, 0.38);
  --shadow-lift: 0 14px 30px rgba(0, 0, 0, 0.55), 0 3px 8px rgba(0, 0, 0, 0.45);
}

/* Felt: a fine two-way weave over a lit green ground, with a vignette so the
   table reads as a surface under a lamp rather than as a flat fill. */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(ellipse 120% 80% at 50% -10%, rgba(94, 178, 124, 0.3), transparent 62%),
    radial-gradient(ellipse 100% 100% at 50% 50%, transparent 45%, rgba(0, 0, 0, 0.55) 100%),
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.022) 0 2px, transparent 2px 4px),
    repeating-linear-gradient(-45deg, rgba(0, 0, 0, 0.05) 0 2px, transparent 2px 4px),
    linear-gradient(180deg, #1a4a32 0%, #123a28 45%, #0b2418 100%);
}

/* ------------------------------------------------------------- skeleton */

#app {
  /* `min-height` alone never caps the column, so the board grows past the
     window and the status bar falls below the fold. */
  height: 100dvh;
  min-height: 420px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 7px 12px;
  background: linear-gradient(180deg, rgba(20, 52, 38, 0.92), rgba(7, 26, 18, 0.88));
  border: 1px solid var(--edge);
  border-radius: var(--radius);
  box-shadow: inset 0 1px 0 rgba(233, 198, 103, 0.12), 0 4px 14px rgba(0, 0, 0, 0.3);
}

.brand {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  display: flex;
  align-items: center;
  gap: 9px;
  color: var(--gold);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.6);
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: linear-gradient(160deg, #c8382e, #8f1f18);
  color: #f2d9a8;
  font-family: serif;
  font-size: 1.05rem;
  box-shadow: inset 0 0 0 1.5px #f2d9a8, 0 2px 6px rgba(0, 0, 0, 0.5);
}

.topbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.field { display: flex; align-items: center; gap: 6px; font-size: 0.78rem; color: var(--ink-dim); }

select {
  background: #0f2a1d;
  border: 1px solid var(--edge);
  border-radius: 8px;
  padding: 5px 8px;
  font-size: 0.82rem;
}

.btn {
  padding: 7px 13px;
  border-radius: 9px;
  border: 1px solid var(--edge-strong);
  background: rgba(255, 255, 255, 0.06);
  font-size: 0.83rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.08s, border-color 0.15s;
}
.btn:hover { background: rgba(255, 255, 255, 0.13); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

.btn-primary { background: linear-gradient(#d9a53c, #b8842a); border-color: #8e6516; color: #241a06; }
.btn-primary:hover { background: linear-gradient(#e8b752, #c8922f); }

.btn-go { background: linear-gradient(#cf3b30, #9d271f); border-color: #7d1c15; color: #fff4ea; }
.btn-go:hover { background: linear-gradient(#e04a3d, #b02d24); }

.btn-stop { background: linear-gradient(#2f7fb5, #1f5d89); border-color: #17486b; color: #f0f7ff; }
.btn-stop:hover { background: linear-gradient(#3b91c9, #256b9b); }

.btn-ghost { border-color: transparent; background: transparent; padding: 4px 8px; }

.board {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}

/* ----------------------------------------------------------------- seat */

.seat {
  flex: 0 0 auto;
  background: linear-gradient(180deg, rgba(18, 48, 35, 0.72), rgba(6, 24, 16, 0.6));
  border: 1px solid var(--edge);
  border-radius: var(--radius);
  padding: 6px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  box-shadow: inset 0 1px 0 rgba(233, 198, 103, 0.08);
}

.seat-me {
  border-color: rgba(233, 198, 103, 0.34);
  box-shadow: inset 0 1px 0 rgba(233, 198, 103, 0.16), 0 0 0 1px rgba(233, 198, 103, 0.06);
}

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

.seat-id { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 0 0 auto; }

.avatar {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #24423a;
  border: 1px solid var(--edge-strong);
  font-size: 0.68rem;
  font-weight: 700;
  flex: 0 0 auto;
}
.avatar-me { background: #4a3a14; border-color: var(--gold-deep); color: var(--gold); }

.seat-name { font-size: 0.86rem; font-weight: 600; }

.go-chip {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--red);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.04em;
}

.seat-score { display: flex; align-items: baseline; gap: 2px; color: var(--gold); flex: 0 0 auto; }
.seat-score strong {
  font-size: 1.6rem;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.55);
}
.seat-score span { font-size: 0.72rem; color: var(--ink-dim); }
.seat-score.is-win strong { color: var(--green); text-shadow: 0 0 14px rgba(79, 177, 115, 0.5); }

/* `minmax(0, 1fr)`, not the default `auto`: an auto track sizes to the row's
   min-content, which for a nowrap hand is ten whole cards, and the row then
   overflows instead of letting the cards shrink. */
.seat-body { display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px; }

/* ----------------------------------------------------------- card faces */

.card {
  position: relative;
  isolation: isolate;
  /* Shrinkable width, auto height: the ratio still resolves the height,
     which it would not if flex made both axes definite. */
  flex: 0 1 var(--card-w);
  width: var(--card-w);
  min-width: 0;
  aspect-ratio: 103.2 / 168.2;   /* the artwork's own ratio */
  border-radius: 7px;
  overflow: hidden;
  background: transparent;
  border: none;
  box-shadow: var(--shadow-card);
  padding: 0;
  cursor: default;
  transition: transform 0.14s ease, box-shadow 0.14s ease, filter 0.14s ease;
}

.card .card-art,
.card .card-img { display: block; width: 100%; height: 100%; }

/* Two play aids painted over the artwork: the month, which is what cards are
   matched on, and a mark for the types the real cards do not distinguish. */
.card-month {
  position: absolute;
  left: 4%;
  bottom: 3%;
  min-width: 26%;
  padding: 1% 4%;
  border-radius: 4px;
  background: rgba(18, 12, 6, 0.82);
  color: #f7e9cc;
  font-size: calc(var(--card-w) * 0.23);
  font-weight: 800;
  line-height: 1.25;
  text-align: center;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

.card-badge {
  position: absolute;
  right: 4%;
  top: 3%;
  display: grid;
  place-items: center;
  width: 30%;
  aspect-ratio: 1;
  border-radius: 4px;
  font-size: calc(var(--card-w) * 0.22);
  font-weight: 800;
  line-height: 1;
  box-shadow: 0 0 0 1px rgba(20, 12, 6, 0.55);
  pointer-events: none;
}
.card-badge.is-godori { background: #d8352c; color: #fff6ea; }
.card-badge.is-animal { background: #2f6fb0; color: #fff; }
.card-badge.is-double { background: #171008; color: #f4cf5e; }
.card-badge.is-ribbon { background: #f2e3bd; color: #6d1109; }
.card-badge.ribbon-cho { color: #16307a; }
.card-badge.ribbon-cheong { background: #2f4fb5; color: #fff; }
.card-badge.ribbon-plain { background: #f2e3bd; color: #6d1109; }

.card.is-playable { cursor: pointer; }
.card.is-playable:hover,
.card.is-playable:focus-visible {
  transform: translateY(-9px);
  box-shadow: var(--shadow-lift);
  outline: none;
  z-index: 3;               /* lift clear of the cards fanned over it */
}
.card.is-playable:focus-visible { box-shadow: var(--shadow-lift), 0 0 0 3px var(--gold); }

.card.is-choice {
  cursor: pointer;
  box-shadow: 0 0 0 3px var(--gold), var(--shadow-lift);
  animation: pulse 1.1s ease-in-out infinite;
}

.card.is-dim { filter: brightness(0.55) saturate(0.6); }

.card.is-fresh { animation: pop 0.34s ease-out; }

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 3px var(--gold), var(--shadow-lift); }
  50% { box-shadow: 0 0 0 6px rgba(233, 198, 103, 0.45), var(--shadow-lift); }
}
@keyframes pop {
  from { transform: scale(0.7) translateY(-14px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

/* ----------------------------------------------------------------- hand */

.hand {
  display: flex;
  flex-wrap: nowrap;         /* ten cards always stay on one row */
  justify-content: center;
  gap: 4px;
  align-items: flex-end;
  padding-top: 10px;         /* room for the hover lift */
  min-height: calc(var(--card-w) * 1.2);
}

/* Overlapping from the left keeps each card's month tag, in its bottom-left
   corner, visible — so a fanned hand is still readable. */
@media (max-width: 560px) {
  .hand { gap: 0; }
  .hand .card + .card { margin-left: calc(var(--card-w) * -0.28); }
}

.hand-back { opacity: 0.94; gap: 0; }
.hand-back .card {
  flex: 0 1 calc(var(--card-w) * 0.72);
  width: calc(var(--card-w) * 0.72);
}
.hand-back .card + .card { margin-left: calc(var(--card-w) * -0.2); }

/* --------------------------------------------------------------- floor  */

.floor-area {
  flex: 1 1 0;
  overflow: hidden;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  padding: 8px 10px;
  background:
    radial-gradient(ellipse 80% 70% at 50% 45%, rgba(120, 200, 150, 0.09), transparent 70%),
    rgba(4, 18, 12, 0.4);
  border: 1px solid var(--edge);
  border-radius: var(--radius);
  min-height: 0;
  box-shadow: inset 0 2px 14px rgba(0, 0, 0, 0.45), inset 0 0 0 1px rgba(233, 198, 103, 0.05);
}

.stock { display: grid; justify-items: center; gap: 5px; }

.stock-card {
  flex: 0 0 auto;
  width: var(--card-w);
  aspect-ratio: 103.2 / 168.2;
  border-radius: 7px;
  overflow: hidden;
  box-shadow:
    2px 2px 0 #6d1710, 3px 3px 0 rgba(0, 0, 0, 0.3),
    5px 5px 0 #6d1710, 6px 6px 0 rgba(0, 0, 0, 0.25),
    8px 9px 16px rgba(0, 0, 0, 0.45);
}
.stock-card.is-empty { border-style: dashed; border-color: var(--edge); box-shadow: none; opacity: 0.4; }
.stock-card .card-art { display: block; width: 100%; height: 100%; }
.stock-card.is-drawing { animation: nudge 0.3s ease; }
@keyframes nudge { 50% { transform: translate(3px, -3px) rotate(-2deg); } }

.stock-count { font-size: 0.7rem; color: var(--ink-dim); }

.floor {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
  align-content: center;
  justify-content: center;
  max-height: 100%;
  overflow-y: auto;
  scrollbar-width: thin;
}
.floor .card { flex: 0 0 auto; }

/* Same-month cards stack so a 뻑 pile reads as one group. */
.floor-group { display: flex; flex: 0 0 auto; }
.floor-group .card + .card { margin-left: calc(var(--card-w) * -0.62); }
.floor-group.is-pile .card { box-shadow: 0 0 0 2px var(--red), 0 0 12px rgba(216, 69, 58, 0.45), var(--shadow-card); }

.action-slots { display: grid; gap: 8px; }

.slot { display: grid; justify-items: center; gap: 4px; }
.slot-label { font-size: 0.64rem; color: var(--ink-dim); letter-spacing: 0.06em; }
.slot-card {
  flex: 0 0 auto;
  width: var(--card-w);
  aspect-ratio: 103.2 / 168.2;
  border-radius: 7px;
  border: 1.5px dashed var(--edge);
  background: rgba(0, 0, 0, 0.16);
  display: grid;
  place-items: center;
  overflow: hidden;
}
.slot-card .card { flex: 0 0 auto; width: 100%; }
.slot-card:empty::after { content: ''; }

/* ---------------------------------------------------------------- piles */

.piles {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 5px;
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
}

.pile {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(0, 0, 0, 0.26);
  border: 1px solid var(--edge);
  border-radius: 9px;
  padding: 3px 6px;
  min-width: 0;
  box-shadow: inset 0 1px 5px rgba(0, 0, 0, 0.4);
  transition: border-color 0.2s, box-shadow 0.2s;
}
.pile.is-scoring {
  border-color: rgba(233, 198, 103, 0.5);
  box-shadow: inset 0 1px 5px rgba(0, 0, 0, 0.4), 0 0 10px rgba(233, 198, 103, 0.12);
}

.pile-head {
  display: flex;
  align-items: baseline;
  gap: 3px;
  font-size: 0.64rem;
  color: var(--ink-dim);
  flex: 0 0 auto;
}
.pile-head b { color: var(--ink); font-size: 0.78rem; font-variant-numeric: tabular-nums; }
.pile.is-scoring .pile-head b { color: var(--gold); }

.pile-cards { display: flex; flex-wrap: nowrap; flex: 1 1 auto; min-width: 0; min-height: calc(var(--card-w) * 0.42); }
.pile-cards .card {
  flex: 0 1 calc(var(--card-w) * 0.46);
  width: calc(var(--card-w) * 0.46);
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.55);
}
.pile-cards .card-month,
.pile-cards .card-badge { display: none; }
.pile-cards .card + .card { margin-left: calc(var(--card-w) * -0.28); }
.pile-cards .card:hover { z-index: 2; transform: translateY(-5px); }

.pile-empty { font-size: 0.66rem; color: rgba(169, 191, 174, 0.4); align-self: center; }

/* --------------------------------------------------------------- status */

.status {
  padding: 6px 14px;
  border-radius: var(--radius);
  background: linear-gradient(180deg, rgba(20, 52, 38, 0.9), rgba(6, 24, 16, 0.85));
  border: 1px solid var(--edge);
  font-size: 0.86rem;
  letter-spacing: 0.02em;
  text-align: center;
  min-height: 34px;
  display: grid;
  place-items: center;
  box-shadow: inset 0 1px 0 rgba(233, 198, 103, 0.1);
  transition: border-color 0.2s, color 0.2s;
}
.status.is-alert {
  border-color: var(--gold);
  color: var(--gold);
  font-weight: 700;
  box-shadow: inset 0 1px 0 rgba(233, 198, 103, 0.2), 0 0 16px rgba(233, 198, 103, 0.14);
}

/* ---------------------------------------------------------------- toast */

.toast {
  position: fixed;
  top: 22%;
  left: 50%;
  transform: translate(-50%, -20px);
  padding: 14px 34px;
  border-radius: 999px;
  background: rgba(10, 30, 20, 0.94);
  border: 2px solid var(--gold);
  color: var(--gold);
  font-size: clamp(1.4rem, 6vw, 2.4rem);
  font-weight: 800;
  letter-spacing: 0.08em;
  box-shadow: 0 16px 44px rgba(0, 0, 0, 0.6);
  opacity: 0;
  pointer-events: none;
  z-index: 40;
}
.toast.is-on { animation: toast 1.05s ease-out; }
.toast.tone-bad { border-color: #7e8b83; color: #c9d6cd; }
.toast.tone-good { border-color: var(--green); color: var(--green); }

@keyframes toast {
  0% { opacity: 0; transform: translate(-50%, 12px) scale(0.82); }
  18% { opacity: 1; transform: translate(-50%, -8px) scale(1.06); }
  75% { opacity: 1; transform: translate(-50%, -8px) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -26px) scale(1); }
}

/* -------------------------------------------------------------- logpanel */

.logpanel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(340px, 86vw);
  background: var(--panel-solid);
  border-left: 1px solid var(--edge-strong);
  padding: 12px 14px;
  overflow-y: auto;
  z-index: 30;
  box-shadow: -14px 0 40px rgba(0, 0, 0, 0.5);
}
.logpanel-head { display: flex; align-items: center; justify-content: space-between; }
.logpanel h2 { margin: 0; font-size: 0.92rem; }
.logpanel ol { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 5px; }
.logpanel li {
  font-size: 0.78rem;
  line-height: 1.45;
  padding: 6px 9px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.045);
  border-left: 3px solid transparent;
}
.logpanel li.tone-good { border-left-color: var(--green); }
.logpanel li.tone-bad { border-left-color: #8b98a0; }
.logpanel li.tone-big { border-left-color: var(--gold); color: var(--gold); font-weight: 600; }

/* --------------------------------------------------------------- dialog */

.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(4, 14, 10, 0.78);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 50;
  backdrop-filter: blur(3px);
}

.dialog {
  width: min(520px, 100%);
  max-height: 86dvh;
  overflow-y: auto;
  background: var(--panel-solid);
  border: 1px solid var(--edge-strong);
  border-radius: 18px;
  padding: 20px 22px;
  box-shadow: 0 30px 70px rgba(0, 0, 0, 0.6);
  animation: rise 0.22s ease-out;
}
@keyframes rise { from { opacity: 0; transform: translateY(14px); } }

.dialog h2 { margin: 0 0 12px; font-size: 1.15rem; color: var(--gold); }
.dialog h3 { margin: 16px 0 6px; font-size: 0.9rem; color: var(--gold); }
.dialog p, .dialog li { font-size: 0.85rem; line-height: 1.6; }
.dialog ul { margin: 6px 0; padding-left: 1.1rem; }
.dialog-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }
.dialog-actions .btn { padding: 10px 20px; font-size: 0.92rem; }

.scoreline { display: flex; justify-content: space-between; gap: 10px; padding: 4px 0; border-bottom: 1px dashed var(--edge); font-size: 0.85rem; }
.scoreline:last-child { border-bottom: 0; }
.scoreline.total { font-weight: 800; color: var(--gold); font-size: 1rem; padding-top: 8px; }
.scoreline .v { font-variant-numeric: tabular-nums; }

.rule-table { width: 100%; border-collapse: collapse; font-size: 0.8rem; margin-top: 6px; }
.rule-table th, .rule-table td { text-align: left; padding: 5px 6px; border-bottom: 1px solid var(--edge); }
.rule-table th { color: var(--ink-dim); font-weight: 600; width: 34%; }

.result-banner { text-align: center; padding: 6px 0 14px; }
.result-banner .big { display: block; font-size: 2.3rem; font-weight: 900; letter-spacing: 0.04em; }
.result-banner.win .big { color: var(--green); }
.result-banner.lose .big { color: var(--red); }
.result-banner.draw .big { color: var(--ink-dim); }
.result-banner .sub { font-size: 0.85rem; color: var(--ink-dim); }

/* ------------------------------------------------------------ responsive */

@media (min-width: 620px) { :root { --card-w: 72px; } }
@media (min-width: 900px) {
  :root { --card-w: 84px; }
  #app { padding: 12px; gap: 10px; }
  .brand { font-size: 1.25rem; }
}
@media (min-width: 1200px) { :root { --card-w: 94px; } }
@media (min-width: 1500px) { :root { --card-w: 104px; } }

@media (max-width: 760px) {
  .seat-head { flex-wrap: wrap; }
  .piles { grid-template-columns: repeat(2, minmax(0, 1fr)); flex-basis: 100%; order: 3; }
}

@media (max-width: 480px) {
  :root { --card-w: 56px; }
  .floor-area { grid-template-columns: auto minmax(0, 1fr); grid-template-rows: auto auto; }
  .action-slots { grid-column: 1 / -1; grid-auto-flow: column; justify-content: center; }
  .topbar { padding: 6px; }
  .brand { font-size: 0.95rem; }
  .btn { padding: 6px 10px; font-size: 0.78rem; }
}

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