:root {
    --bg: #e7dcc2;
    --panel: #f8f0dc;
    --panel-border: #2b2417;
    --ink: #201910;
    --accent: #6f5329;
    --grid: #000000;
    --blocked: #4b4b4b;
    --playable: #ffffff;
    --board-bg: #1d1710;
    --tile-bg: #e7d2ac;
    --tile-border: #3b2f22;
    --muted: #7a6c56;
    --shadow: 0 12px 30px rgba(33, 23, 13, 0.18);
    --water: #2579e8;
    --metal: #7f8790;
    --wood: #2f9b46;
    --fire: #d64332;
    --earth: #d0ac2e;
}

* {
    box-sizing: border-box;
}

html, body, #root {
    margin: 0;
    min-height: 100%;
    font-family: Georgia, "Palatino Linotype", serif;
    color: var(--ink);
    background:
        radial-gradient(circle at top left, rgba(255, 255, 255, 0.6), transparent 30%),
        linear-gradient(180deg, #f4e8cc 0%, #dec89f 100%);
}

button, input {
    font: inherit;
}

.app-shell {
    height: 100vh;
    padding: 20px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.topbar {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: center;
    margin-bottom: 16px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
}

.brand img {
    width: 56px;
    height: 56px;
    object-fit: contain;
}

.brand h1 {
    margin: 0;
    font-size: 1.7rem;
}

.brand p {
    margin: 2px 0 0;
    color: var(--muted);
}

.panel {
    background: rgba(248, 240, 220, 0.92);
    border: 2px solid var(--panel-border);
    border-radius: 18px;
    box-shadow: var(--shadow);
}

.lobby {
    max-width: 1024px;
    margin: 0 auto;
    display: grid;
    grid-template-columns: 1.05fr 0.95fr;
    gap: 18px;
}

.lobby-card,
.status-card {
    padding: 20px;
}

.lobby-actions {
    display: grid;
    gap: 14px;
}

.field-row {
    display: grid;
    gap: 8px;
}

.field-row input {
    padding: 12px 14px;
    border-radius: 12px;
    border: 2px solid #8f7653;
    background: #fffef9;
}

.button-row {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.primary-button,
.secondary-button,
.icon-button,
.mini-button {
    border: 2px solid var(--panel-border);
    background: linear-gradient(180deg, #fff9ec 0%, #eed3a6 100%);
    color: var(--ink);
    border-radius: 12px;
    cursor: pointer;
}

.primary-button,
.secondary-button {
    padding: 11px 16px;
    min-width: 130px;
}

.secondary-button {
    background: linear-gradient(180deg, #f3f0e7 0%, #cfc1a6 100%);
}

.primary-button:disabled,
.secondary-button:disabled,
.icon-button:disabled,
.mini-button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.game-layout {
    display: grid;
    grid-template-columns: minmax(0, 70%) minmax(280px, 30%);
    gap: 18px;
    align-items: stretch;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

.board-panel {
    padding: 16px;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: rgba(248, 240, 220, 0.92);
    background-image:
        linear-gradient(rgba(248, 240, 220, 0.7), rgba(248, 240, 220, 0.7)),
        url('./assets/bg.png');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}

.board-grid {
    display: grid;
    grid-template-columns: repeat(7, minmax(42px, 1fr));
    gap: 4px;
    background: rgba(0, 0, 0, 0.5);
    padding: 4px;
    border-radius: 18px;
    height: min(100%, calc(100vh - 220px));
    width: auto;
    max-width: 100%;
    aspect-ratio: 7 / 9;
    margin: 0;
}

.board-cell {
    position: relative;
    aspect-ratio: 1;
    min-width: 42px;
    border-radius: 12px;
    border: 1px solid rgba(0, 0, 0, 0.55);
    background: rgba(255, 255, 255, 0.74);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 120ms ease, box-shadow 120ms ease, background 120ms ease;
}

.board-cell.blocked {
    background: rgba(75, 75, 75, 0.78);
}

.board-cell.camp::after,
.board-cell.king::after {
    position: absolute;
    top: 6px;
    left: 7px;
    font-size: 0.68rem;
    color: #5f5136;
    font-weight: 700;
}

.board-cell.camp::after {
    content: "C";
}

.board-cell.king::after {
    content: "K";
}

.board-cell.planet::before {
    content: "";
    position: absolute;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: 2px solid #4b3f28;
    background: transparent;
    top: 8px;
    left: 8px;
}

.board-cell.highlight {
    box-shadow: inset 0 0 0 4px rgba(30, 132, 73, 0.55);
}

.board-cell.target {
    box-shadow: inset 0 0 0 4px rgba(171, 27, 47, 0.55);
}

.board-cell.drop-ready {
    transform: translateY(-2px);
    box-shadow: inset 0 0 0 4px rgba(26, 89, 160, 0.6);
}

.side-column {
    display: grid;
    gap: 16px;
    min-height: 0;
    grid-template-rows: minmax(0, 0.2fr) minmax(0, 0.2fr) minmax(0, 0.6fr);
    overflow: hidden;
}

.action-panel,
.status-panel,
.hand-panel,
.log-panel,
.setup-panel {
    padding: 16px;
}

.hand-panel {
    min-height: 0;
    overflow: hidden;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
}

.action-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(70px, 1fr));
    gap: 10px;
}

.icon-button {
    min-height: 52px;
    padding: 10px 12px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.icon-button.selected {
    box-shadow: inset 0 0 0 3px #214f7f;
}

.icon-sprite,
.stat-sprite {
    background-repeat: no-repeat;
    image-rendering: auto;
}

.icon-sprite {
    width: 16px;
    height: 16px;
    background-image: url('./assets/action-icons.png');
    background-size: 96px 16px;
}

.icon-sprite.summon { background-position: 0 0; }
.icon-sprite.attack { background-position: -16px 0; }
.icon-sprite.transform { background-position: -32px 0; }
.icon-sprite.remove { background-position: -48px 0; }
.icon-sprite.discard { background-position: -64px 0; }
.icon-sprite.finishTurn { background-position: -80px 0; }

.action-label {
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.hand-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-rows: repeat(5, minmax(0, 1fr));
    gap: 4%;
    align-content: stretch;
    align-items: center;
    justify-items: center;
    height: 100%;
    overflow: hidden;
}

.tile {
    position: relative;
    width: 96px;
    height: 96px;
    flex: 0 0 auto;
    font-size: 14px;
    border-radius: 16px;
    border: 2px solid var(--tile-border);
    background:
        linear-gradient(180deg, rgba(255, 255, 255, 0.32), transparent 28%),
        linear-gradient(180deg, #efdcbc 0%, var(--tile-bg) 100%);
    box-shadow: 0 8px 18px rgba(62, 44, 22, 0.22);
    overflow: hidden;
    cursor: pointer;
}

.hand-grid .tile {
    aspect-ratio: 1;
    min-width: 0;
    min-height: 0;
    font-size: clamp(8px, 0.9vw, 13px);
    place-self: center;
}

.tile.glowing {
    animation: chainGlow 1.6s ease-in-out infinite;
}

.tile.rotated {
    transform: rotate(180deg);
}

.tile.selected {
    box-shadow: 0 0 0 4px rgba(39, 92, 153, 0.38), 0 8px 18px rgba(62, 44, 22, 0.22);
}

.tile.unplayable {
    filter: grayscale(0.65) brightness(0.7);
    opacity: 0.75;
}

.tile.board-tile {
    width: min(100%, 72px);
    height: min(100%, 72px);
    font-size: clamp(8px, 0.8vw, 11px);
    cursor: pointer;
}

.owner-dot {
    position: absolute;
    top: 40%;
    left: 50%;
    width: 1.45em;
    height: 1.45em;
    border-radius: 50%;
    transform: translate(-50%, -50%);
    border: 0.14em solid var(--owner-border, #111);
    background: #111;
}

.owner-dot.opponent {
    background: #fff;
    box-shadow: inset 0 0 0 0.14em var(--owner-border, #111);
}

.tile-stats {
    position: absolute;
    bottom: 0.9em;
    left: 0.55em;
    right: 0.55em;
    display: flex;
    justify-content: center;
    gap: 0.5em;
    flex-wrap: nowrap;
    white-space: nowrap;
}

.tile-stat {
    display: flex;
    align-items: center;
    gap: 0.28em;
    font-size: 0.68em;
    font-weight: 700;
    min-width: 0;
}

.tile-stat-letter {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1em;
}

.direction-marker {
    position: absolute;
    width: 14%;
    height: 14%;
    background: var(--marker-color);
    clip-path: polygon(50% 0%, 100% 100%, 0% 100%);
}

.direction-marker.N { top: 3%; left: 50%; transform: translateX(-50%); }
.direction-marker.NE { top: 3%; right: 3%; transform: rotate(45deg); }
.direction-marker.E { top: 50%; right: 2%; transform: translateY(-50%) rotate(90deg); }
.direction-marker.SE { bottom: 3%; right: 3%; transform: rotate(135deg); }
.direction-marker.S { bottom: 2%; left: 50%; transform: translateX(-50%) rotate(180deg); }
.direction-marker.SW { bottom: 3%; left: 3%; transform: rotate(225deg); }
.direction-marker.W { top: 50%; left: 2%; transform: translateY(-50%) rotate(270deg); }
.direction-marker.NW { top: 3%; left: 3%; transform: rotate(315deg); }

.tile-meta,
.status-grid,
.setup-grid {
    display: grid;
    gap: 10px;
}

.status-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.status-card {
    padding: 12px;
}

.log-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: grid;
    gap: 8px;
    max-height: 240px;
    overflow-y: auto;
    font-size: 0.92rem;
}

.log-drawer {
    position: fixed;
    top: 0;
    right: 0;
    height: 100vh;
    width: min(420px, 92vw);
    z-index: 20;
    border-radius: 0;
    transform: translateX(100%);
    transition: transform 180ms ease;
    padding: 20px;
    display: grid;
    grid-template-rows: auto 1fr;
    gap: 12px;
}

.log-drawer.open {
    transform: translateX(0);
}

.log-drawer-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

.log-list li {
    border-bottom: 1px solid rgba(67, 53, 31, 0.18);
    padding-bottom: 6px;
}

.setup-order {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.king-chip {
    border: 2px solid var(--panel-border);
    border-radius: 14px;
    padding: 10px;
    min-width: 140px;
    background: rgba(255, 253, 247, 0.9);
    display: grid;
    gap: 8px;
}

.chip-controls {
    display: flex;
    justify-content: space-between;
    gap: 8px;
}

.mini-button {
    padding: 7px 10px;
}

.hint {
    color: var(--muted);
    font-size: 0.92rem;
}

.toast {
    position: fixed;
    right: 18px;
    bottom: 18px;
    z-index: 30;
    max-width: 360px;
    padding: 14px 16px;
}

.inline-row {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}

.pill {
    border-radius: 999px;
    border: 1px solid #6f5b3b;
    padding: 5px 10px;
    background: rgba(255, 252, 247, 0.84);
    font-size: 0.85rem;
}

@keyframes chainGlow {
    0%, 100% {
        box-shadow: 0 0 0 0 rgba(34, 167, 87, 0.0), 0 8px 18px rgba(62, 44, 22, 0.22);
    }
    50% {
        box-shadow: 0 0 0 6px rgba(34, 167, 87, 0.18), 0 0 18px rgba(34, 167, 87, 0.42), 0 8px 18px rgba(62, 44, 22, 0.22);
    }
}

@media (max-width: 960px) {
    .lobby,
    .game-layout {
        grid-template-columns: 1fr;
    }

    .side-column {
        order: -1;
    }

    .board-grid {
        width: auto;
        height: min(100%, calc(100vh - 260px));
    }
}

@media (max-width: 720px) {
    .app-shell {
        padding: 12px;
    }

    .topbar {
        flex-direction: column;
        align-items: stretch;
    }

    .game-layout {
        display: flex;
        flex-direction: column;
        height: auto;
        overflow: visible;
    }

    .side-column {
        order: 0;
    }

    .action-panel {
        order: -1;
    }

    .action-grid {
        grid-template-columns: repeat(6, minmax(0, 1fr));
    }

    .icon-button {
        min-height: 74px;
    }

    .board-panel {
        padding: 12px;
    }

    .board-grid {
        width: 100%;
        max-width: 100%;
        height: auto;
    }

    .tile {
        width: 88px;
        height: 88px;
    }

    .tile.board-tile {
        width: 100%;
        height: 100%;
    }
}
