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

:root {
    --bg: #0d1a14;
    --felt: #17402f;
    --felt-2: #10312333;
    --panel: #1a2b23;
    --line: #2f4d3e;
    --text: #eef6f1;
    --muted: #9ab8a9;
    --accent: #f0c14b;
    --tile: #f6f2e7;
    --tile-edge: #cdc5b0;
    --wan: #b8352c;
    --tong: #2f6fb8;
    --tiao: #2f8a4d;
    --honour: #2b2b2b;
}

html, body { height: 100%; overscroll-behavior: none; }

body {
    position: fixed; inset: 0; overflow: hidden;
    color: var(--text);
    font-family: 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Segoe UI', sans-serif;
    background:
        radial-gradient(circle at 50% 40%, #1d5540, #0c2018 70%),
        linear-gradient(180deg, #08150f, #0d1a14);
    user-select: none; -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.shell {
    display: flex; flex-direction: column;
    height: 100%; min-height: 100dvh;
    max-width: 560px; margin: 0 auto;
    padding:
        max(6px, env(safe-area-inset-top))
        max(6px, env(safe-area-inset-right))
        max(8px, env(safe-area-inset-bottom))
        max(6px, env(safe-area-inset-left));
}

.toolbar { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; height: 38px; }
.toolbar-spacer { flex: 1; }

.icon-btn {
    display: inline-flex; align-items: center; justify-content: center;
    min-width: 34px; height: 31px; padding: 0 10px;
    border: 1px solid var(--line); border-radius: 9px;
    background: var(--panel); color: var(--text);
    font-family: inherit; font-size: 0.88rem; line-height: 1;
    text-decoration: none; cursor: pointer;
}
.icon-btn-text { font-size: 0.76rem; font-weight: 600; }
.icon-btn:active { transform: scale(0.94); border-color: var(--accent); }

.hud { display: flex; gap: 5px; flex: 0 0 auto; margin: 5px 0; }
.hud span {
    flex: 1; display: flex; flex-direction: column; align-items: center;
    padding: 4px 2px; border-radius: 9px; background: rgba(255, 255, 255, 0.06);
}
.hud b { font-size: 0.86rem; font-variant-numeric: tabular-nums; }
.hud b.is-you { color: var(--accent); }
.hud i { font-style: normal; font-size: 0.56rem; letter-spacing: 0.08em; color: var(--muted); }

/* The table: three opponents round the top and sides, you along the bottom. */
.table { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; gap: 4px; }

.seat {
    flex: 0 0 auto; display: flex; align-items: center; gap: 6px;
    padding: 4px 6px; border-radius: 10px;
    background: rgba(255, 255, 255, 0.04);
    font-size: 0.66rem; color: var(--muted);
}
.seat.is-turn { background: rgba(240, 193, 75, 0.16); color: var(--text); }
.seat b { font-size: 0.72rem; color: var(--text); }
.seat .back { display: inline-flex; gap: 1px; }
.seat .back i {
    width: 6px; height: 13px; border-radius: 2px;
    background: linear-gradient(180deg, #2c6b52, #1d4b39);
    display: inline-block;
}
.seat .melds { display: flex; gap: 4px; flex-wrap: wrap; }

/* Everything that has been thrown away, which is what a player reads. */
.pool {
    flex: 1 1 auto; min-height: 40px; overflow-y: auto;
    display: flex; flex-wrap: wrap; align-content: flex-start; gap: 2px;
    padding: 5px; border-radius: 10px;
    background: rgba(0, 0, 0, 0.22);
}
.pool .tile { transform: scale(0.72); transform-origin: top left; margin: 0 -6px -8px 0; }
.pool .tile.is-last { outline: 2px solid var(--accent); }

.tile {
    position: relative;
    width: 30px; height: 42px; flex: 0 0 auto;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    border-radius: 5px;
    background: linear-gradient(180deg, #fbf8ef, var(--tile));
    border: 1px solid var(--tile-edge);
    box-shadow: 0 2px 0 #b9b1a0;
    color: var(--honour);
    font-weight: 800; line-height: 1;
    cursor: pointer;
}
.tile .rank { font-size: 0.95rem; }
.tile .suit { font-size: 0.52rem; opacity: 0.8; margin-top: 1px; }
.tile .whole { font-size: 0.95rem; }
.tile.wan { color: var(--wan); }
.tile.tong { color: var(--tong); }
.tile.tiao { color: var(--tiao); }
.tile.is-drawn { margin-left: 10px; box-shadow: 0 2px 0 #b9b1a0, 0 0 0 2px var(--accent); }
.tile:active { transform: translateY(2px); box-shadow: 0 0 0 #b9b1a0; }

/* Fourteen tiles have to sit on one line on a phone held upright, so the ones
   in hand are narrower than the ones on the table. Wrapping them put the tile
   just drawn on a line of its own, which is exactly the tile being looked at. */
.hand { flex: 0 0 auto; display: flex; gap: 1px; justify-content: center; padding: 4px 0; }
.hand .tile { width: 23px; height: 34px; }
.hand .tile .rank { font-size: 0.8rem; }
.hand .tile .suit { font-size: 0.44rem; }
.hand .tile .whole { font-size: 0.78rem; }
.hand .tile.is-drawn { margin-left: 7px; }
.myMelds { flex: 0 0 auto; display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; min-height: 0; }
.myMelds .set { display: flex; gap: 1px; }
.myMelds .tile { transform: scale(0.66); transform-origin: bottom center; margin: 0 -5px; }

/* Only what can actually be done shows up here. */
.calls { display: flex; gap: 6px; justify-content: center; flex: 0 0 auto; min-height: 46px; margin-top: 4px; }
.calls button {
    min-width: 58px; min-height: 44px; padding: 0 12px;
    border: 1px solid var(--line); border-radius: 11px;
    background: var(--panel); color: var(--text);
    font-family: inherit; font-size: 0.95rem; font-weight: 700; cursor: pointer;
}
.calls button.win { border-color: var(--accent); background: rgba(240, 193, 75, 0.24); color: var(--accent); }
.calls button:active { transform: scale(0.96); }

.hint-text { margin-top: 4px; text-align: center; font-size: 0.66rem; color: var(--muted); min-height: 1.2em; }
.hint-text.is-warn { color: var(--accent); font-weight: 700; }

.overlay {
    position: absolute; inset: 0; display: none;
    align-items: center; justify-content: center; padding: 18px;
    background: rgba(5, 12, 9, 0.9);
    backdrop-filter: blur(5px); -webkit-backdrop-filter: blur(5px);
    z-index: 50;
}
.overlay.is-open { display: flex; }

.panel {
    width: 100%; max-width: 340px; max-height: 100%; overflow-y: auto;
    display: flex; flex-direction: column; gap: 10px;
    padding: 22px 18px;
    border: 1px solid var(--line); border-radius: 20px;
    background: linear-gradient(180deg, #204133, #0e1d16);
    text-align: center; animation: pop 0.22s ease;
}
@keyframes pop { from { opacity: 0; transform: translateY(12px) scale(0.97); } to { opacity: 1; transform: none; } }

.title {
    font-size: clamp(1.8rem, 8vw, 2.4rem); font-weight: 800; letter-spacing: 0.12em;
    background: linear-gradient(135deg, #ffe9ac, var(--accent) 55%, #d98b2b);
    -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.panel-title { font-size: 1.5rem; font-weight: 800; color: var(--accent); }
.subtitle { color: var(--muted); font-size: 0.85rem; line-height: 1.7; }

.modes { display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
.mode {
    display: flex; align-items: center; justify-content: space-between;
    padding: 13px 16px;
    border: 1px solid var(--line); border-radius: 13px;
    background: rgba(255, 255, 255, 0.05); color: var(--text);
    font-family: inherit; font-size: 0.95rem; font-weight: 700; cursor: pointer;
}
.mode small { font-size: 0.68rem; font-weight: 500; color: var(--muted); }
.mode:active { transform: scale(0.98); border-color: var(--accent); }

.legend { display: flex; flex-direction: column; gap: 5px; margin-top: 6px; text-align: left; }
.legend h3 { font-size: 0.7rem; letter-spacing: 0.1em; color: var(--muted); font-weight: 600; }
.legend-row { display: flex; align-items: baseline; gap: 8px; font-size: 0.72rem; color: var(--muted); }
.legend-row b { flex: 0 0 auto; width: 2.4em; color: var(--text); font-size: 0.78rem; }

.btn {
    min-height: 46px; padding: 12px 20px;
    border: 1px solid var(--line); border-radius: 13px;
    background: rgba(255, 255, 255, 0.05); color: var(--text);
    font-family: inherit; font-size: 0.92rem; font-weight: 700;
    display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.btn:active { transform: scale(0.97); }
.btn-primary { border-color: transparent; background: linear-gradient(135deg, #ffe6a8, #dda63a); color: #2a1c00; }

.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
.stat { padding: 9px 6px; border-radius: 11px; background: rgba(255, 255, 255, 0.06); }
.stat b { display: block; font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.stat span { font-size: 0.62rem; color: var(--muted); }

@media (max-height: 720px) {
    .hud { margin: 3px 0; }
    .tile { width: 26px; height: 37px; }
    .tile .rank { font-size: 0.85rem; }
    .hint-text { display: none; }
}

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