:root {
  --felt: #173d2e;
  --felt-2: #1e4c39;
  --felt-3: #275d47;
  --ink: #f3efe4;
  --muted: #a9bdb2;
  --accent: #f0b84a;
  --red: #e2504f;
  --green: #4fc27f;
  --tile: #fbf7ec;
  --tile-edge: #d8cfb8;
  --tile-back: #2f8f5b;
  --radius: 12px;
  /* 6 hand tiles + gaps must fit inside #me at any phone width */
  --hand-w: min(calc((min(100vw, 560px) - 84px) / 6.6), 62px);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(ellipse at 50% 30%, var(--felt-2), var(--felt) 70%) fixed;
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, 'Pretendard', 'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
button, input { font: inherit; color: inherit; }
[hidden] { display: none !important; }

#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100%;
  padding: env(safe-area-inset-top) 12px calc(16px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: center;
  height: 52px;
  gap: 8px;
}
header h1 {
  flex: 1;
  margin: 0;
  font-size: 20px;
  letter-spacing: .02em;
  display: flex;
  align-items: center;
  gap: 6px;
}
#logo { display: inline-flex; width: 22px; height: 30px; background: var(--tile); border-radius: 4px; padding: 2px; box-shadow: 0 2px 0 var(--tile-edge); }
#logo svg { width: 100%; height: 100%; }
.header-right { display: flex; gap: 6px; }
.icon-btn {
  width: 36px; height: 36px;
  border: 0; border-radius: 50%;
  background: rgba(255, 255, 255, .08);
  font-size: 17px; cursor: pointer;
}

button { cursor: pointer; }
.primary, #btn-join, .seg button, #btn-pass {
  border: 0;
  border-radius: var(--radius);
  padding: 13px 18px;
  font-weight: 700;
  background: rgba(255, 255, 255, .12);
}
.primary { background: var(--accent); color: #2a1d05; }
.primary:disabled { opacity: .45; cursor: default; }
.primary.danger { background: var(--red); color: #fff; }
.link { background: none; border: 0; color: var(--muted); text-decoration: underline; padding: 8px; }
.muted { color: var(--muted); }

/* ---------- lobby ---------- */
#lobby { display: flex; flex-direction: column; gap: 14px; padding-top: 8vh; }
.tagline { text-align: center; color: var(--muted); margin: 0 0 12px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
input {
  border: 1px solid rgba(255, 255, 255, .15);
  background: rgba(0, 0, 0, .2);
  border-radius: var(--radius);
  padding: 13px 14px;
  font-size: 16px;
  outline: none;
  user-select: text;
}
input:focus { border-color: var(--accent); }
.or { text-align: center; color: var(--muted); font-size: 13px; }
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; min-width: 0; text-transform: uppercase; letter-spacing: .2em; }

/* ---------- waiting room ---------- */
#waiting { display: flex; flex-direction: column; gap: 12px; padding-top: 12px; }
#waiting h2 { font-size: 16px; margin: 8px 0 0; }
#waiting h2 span { color: var(--muted); font-weight: 400; }
.chip {
  align-self: center;
  border: 1px dashed rgba(255, 255, 255, .3);
  background: none;
  border-radius: 999px;
  padding: 10px 16px;
}
.chip b { letter-spacing: .15em; color: var(--accent); }
#wait-players { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
#wait-players li { display: flex; align-items: center; gap: 8px; background: rgba(0, 0, 0, .18); padding: 12px 14px; border-radius: var(--radius); }
#wait-players li.offline { opacity: .5; }
#host-controls { display: flex; flex-direction: column; gap: 10px; margin-top: 8px; }
.seg { display: flex; gap: 6px; }
.seg button { flex: 1; padding: 10px; font-weight: 600; border: 2px solid transparent; }
.seg button small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
.seg button.on { border-color: var(--accent); }
.hint { text-align: center; color: var(--muted); font-size: 14px; }

.name { font-weight: 700; display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.name::before { content: ''; width: 9px; height: 9px; border-radius: 50%; background: var(--c); flex: none; }
.badge { font-size: 11px; padding: 2px 6px; border-radius: 6px; background: rgba(255, 255, 255, .14); }
.badge.host { background: rgba(240, 184, 74, .25); color: var(--accent); }
.badge.dealer { background: var(--red); color: #fff; font-weight: 700; }

/* ---------- tiles ---------- */
.tile {
  --w: 34px;
  width: var(--w);
  height: calc(var(--w) * 1.4);
  background: var(--tile);
  border-radius: calc(var(--w) * .14);
  box-shadow: 0 calc(var(--w) * .08) 0 var(--tile-edge), 0 calc(var(--w) * .1) calc(var(--w) * .12) rgba(0, 0, 0, .35);
  padding: calc(var(--w) * .06);
  flex: none;
  position: relative;
  transition: transform .12s;
}
.tile svg { display: block; width: 100%; height: 100%; }
.tile.back { background: linear-gradient(160deg, #3aa36b, var(--tile-back)); box-shadow: 0 calc(var(--w) * .08) 0 #1f6b42; }
.tile.xs { --w: 11px; }
.tile.sm { --w: 22px; }
.tile.md { --w: 34px; }
.tile.lg { --w: var(--hand-w); }
.tile.xl { --w: 52px; }
.tile.is-dora::after {
  content: '';
  position: absolute;
  top: -3px; right: -3px;
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 1.5px var(--felt);
}
.tile.last { outline: 2px solid var(--accent); outline-offset: 1px; animation: pop .25s; }
@keyframes pop { from { transform: scale(1.35); } }

/* ---------- game ---------- */
#game { display: flex; flex-direction: column; gap: 10px; flex: 1; }
.info-bar { display: flex; align-items: center; justify-content: space-between; font-size: 14px; padding: 2px 2px 0; }
.info-bar .warn { color: var(--red); }
.dora-box { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }

#opponents { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
#opponents[data-n="1"] { grid-template-columns: 1fr; }
#opponents[data-n="3"] .opp:first-child { grid-column: 1 / -1; }
.opp, #me {
  background: rgba(0, 0, 0, .18);
  border: 2px solid transparent;
  border-radius: var(--radius);
  padding: 8px 10px;
  min-width: 0;
  transition: border-color .2s, background .2s;
}
.opp.active, #me.active { border-color: var(--accent); background: rgba(240, 184, 74, .08); }
.opp.offline { opacity: .55; }
.opp-head, .me-head { display: flex; align-items: center; gap: 6px; font-size: 14px; min-width: 0; }
.pts { margin-left: auto; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; }
.backs { display: flex; gap: 2px; margin: 6px 0; }
.discards { display: flex; flex-wrap: wrap; gap: 3px 3px; min-height: 31px; padding-top: 3px; }

#me { display: flex; flex-direction: column; gap: 8px; }
#status { text-align: center; font-size: 14px; color: var(--muted); min-height: 20px; }
#status.mine { color: var(--accent); font-weight: 700; }
#center {
  flex: 1;
  min-height: 96px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 14px;
  color: var(--muted);
}
#center .tile { animation: pop .25s; }
#my-hand { display: flex; justify-content: center; gap: 4px; padding: 10px 0 8px; min-height: calc(var(--hand-w) * 1.4 + 22px); }
#my-hand .tile.drawn { margin-left: 10px; }
#my-hand .tile.playable { cursor: pointer; }
#my-hand .tile.selected { transform: translateY(-14px); box-shadow: 0 5px 0 var(--tile-edge), 0 0 0 3px var(--accent); }
.actions { display: flex; justify-content: center; }
.actions .primary { min-width: 60%; font-size: 17px; }

#claim-bar {
  background: rgba(226, 80, 79, .15);
  border: 2px solid var(--red);
  border-radius: var(--radius);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  animation: pop .2s;
}
#claim-text { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 4px; font-size: 15px; }
.claim-actions { display: flex; gap: 8px; }
.claim-actions button { flex: 1; }
#btn-ron { font-size: 18px; }

/* ---------- result ---------- */
#result {
  position: fixed;
  inset: 0;
  background: rgba(5, 18, 12, .72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 10;
  animation: fade .2s;
}
@keyframes fade { from { opacity: 0; } }
.result-card {
  width: 100%;
  max-width: 420px;
  max-height: 100%;
  overflow-y: auto;
  background: var(--felt-3);
  border-radius: 18px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .4);
}
#result-title { margin: 0; text-align: center; font-size: 22px; display: flex; justify-content: center; align-items: center; gap: 6px; flex-wrap: wrap; }
#result-title small { font-size: 14px; color: var(--muted); font-weight: 400; }
#result-hand { display: flex; justify-content: center; gap: 12px; }
.meld { display: flex; gap: 3px; }
#result-items, #result-scores, #final-list { list-style: none; margin: 0; padding: 0; }
#result-items li { display: flex; justify-content: space-between; padding: 4px 2px; font-size: 15px; }
#result-items li.total { border-top: 1px solid rgba(255, 255, 255, .2); margin-top: 4px; padding-top: 8px; font-size: 17px; }
#result-items li.total b { color: var(--accent); }
#result-scores li, #final-list li { display: flex; align-items: center; gap: 8px; padding: 6px 2px; }
#result-scores .pts { color: var(--ink); }
.delta { width: 44px; text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--muted); }
.up { color: var(--green) !important; }
.down { color: var(--red) !important; }
#result-others summary { color: var(--muted); font-size: 14px; cursor: pointer; }
.reveal { display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 14px; }
.reveal .name { width: 70px; }
.reveal-tiles { display: flex; gap: 3px; }
#final h3 { margin: 0 0 4px; font-size: 16px; }
#final-list { counter-reset: rank; }
#final-list li { counter-increment: rank; }
#final-list li::before { content: counter(rank) '위'; width: 28px; color: var(--muted); font-size: 13px; }
#final-list li:first-child::before { content: '👑'; }
#final-list li b { margin-left: auto; }

/* ---------- dialogs ---------- */
dialog {
  border: 0;
  border-radius: 18px;
  background: var(--felt-3);
  color: var(--ink);
  width: min(92vw, 480px);
  max-height: 86vh;
  padding: 18px;
}
dialog::backdrop { background: rgba(0, 0, 0, .55); }
dialog h2 { margin: 0 0 8px; font-size: 19px; }
dialog h3 { margin: 16px 0 4px; font-size: 15px; color: var(--accent); }
dialog p { margin: 4px 0; font-size: 14px; line-height: 1.6; }
dialog form { margin-top: 16px; }
dialog form button { width: 100%; }
.red { color: var(--red); }
.pill-btn {
  height: 36px;
  padding: 0 14px;
  border: 1px solid rgba(240, 184, 74, .5);
  border-radius: 999px;
  background: rgba(240, 184, 74, .12);
  color: var(--accent);
  font-weight: 700;
  font-size: 14px;
}
dialog.sheet { padding-top: 14px; }
dialog.sheet h3:first-of-type { margin-top: 6px; }
.yaku-item {
  background: rgba(0, 0, 0, .16);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-top: 8px;
}
.yaku-item.big { background: rgba(240, 184, 74, .1); box-shadow: inset 0 0 0 1px rgba(240, 184, 74, .3); }
.yaku-head { display: flex; justify-content: space-between; align-items: baseline; font-size: 15px; }
.yaku-pts { color: var(--accent); font-weight: 700; font-size: 14px; }
.yaku-tiles { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 8px; }
.yaku-tiles .tile { --w: 28px; }
.yaku-tiles .arrow { font-size: 12px; color: var(--muted); }
dialog p.yaku-desc { margin: 8px 0 0; font-size: 13px; color: var(--muted); line-height: 1.5; }
.score-table, .log-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.score-table td { padding: 5px 2px; border-bottom: 1px solid rgba(255, 255, 255, .08); }
.score-table td:last-child { text-align: right; white-space: nowrap; }
.log-table th, .log-table td { padding: 5px 3px; text-align: center; border-bottom: 1px solid rgba(255, 255, 255, .08); font-variant-numeric: tabular-nums; }
.log-table th .name { font-size: 12px; max-width: 60px; }
.log-table tr.total td { font-weight: 700; border-bottom: 0; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  background: #0c1f17;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: .2s;
  z-index: 20;
  max-width: 90vw;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
