:root {
    --bg: #05070d;
    --panel: rgba(12, 17, 30, 0.92);
    --text: #e6ebf5;
    --muted: #9aa6bd;
    --line: rgba(120, 140, 200, 0.35);
    --team-0: #4f8ff7;
    --team-1: #f59e0b;
    --team-2: #3fbf6f;
    --team-3: #b07cf0;
    --ok: #58d68d;
    --warn: #e74c3c;
    --accent: #7aa7ff;
    color-scheme: dark;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    height: 100%;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

#game {
    position: fixed;
    inset: 0;
    touch-action: none;
    cursor: crosshair;
}

#minimap {
    position: fixed;
    top: max(10px, env(safe-area-inset-top));
    right: max(10px, env(safe-area-inset-right));
    width: 150px;
    height: 150px;
    border-radius: 6px;
    pointer-events: none;
}

body.large-map #minimap {
    top: 50%;
    left: 50%;
    right: auto;
    width: min(80vmin, 640px, max(280px, calc(100vw - 440px)));
    height: min(80vmin, 640px, max(280px, calc(100vw - 440px)));
    transform: translate(-50%, -50%);
}

#hud {
    position: fixed;
    top: max(10px, env(safe-area-inset-top));
    left: max(10px, env(safe-area-inset-left));
    width: 190px;
    pointer-events: none;
}

.bar {
    position: relative;
    height: 16px;
    margin-bottom: 3px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 3px;
    overflow: hidden;
}

.bar .fill {
    position: absolute;
    inset: 0 auto 0 0;
    background: var(--ok);
    opacity: 0.55;
}

#bar-shield .fill {
    background: var(--accent);
}

#bar-shield.off .fill {
    opacity: 0.2;
}

#bar-energy .fill {
    background: #e5c158;
}

#bar-wheat .fill,
#bar-eheat .fill {
    background: #e67e22;
}

.bar.warn .fill {
    background: var(--warn);
    opacity: 0.8;
}

.bar .label {
    position: relative;
    padding-left: 6px;
    font-size: 11px;
    line-height: 16px;
    text-shadow: 0 1px 2px #000;
}

#stats {
    margin-top: 4px;
    font-size: 12px;
    color: var(--text);
    text-shadow: 0 1px 2px #000;
}

#score {
    position: fixed;
    top: max(10px, env(safe-area-inset-top));
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 14px;
    font-size: 13px;
    pointer-events: none;
    text-shadow: 0 1px 2px #000;
    white-space: nowrap;
}

.team-0 {
    color: var(--team-0);
}

.team-1 {
    color: var(--team-1);
}

.team-2 {
    color: var(--team-2);
}

.team-3 {
    color: var(--team-3);
}

.team.out {
    opacity: 0.5;
    text-decoration: line-through;
}

#rooms {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-bottom: 8px;
    max-height: 160px;
    overflow: auto;
}

#rooms button {
    text-align: left;
    padding: 6px 10px;
    border-radius: 6px;
    border: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.04);
}

#rooms button.chosen {
    border-color: var(--accent);
    background: rgba(122, 167, 255, 0.15);
}

#rooms button span {
    display: block;
    font-size: 12px;
    color: var(--muted);
}

.create {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
    margin-bottom: 4px;
}

.create select,
.create button {
    padding: 6px 10px;
    border-radius: 6px;
    border: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.06);
    color: var(--text);
    font: inherit;
}

.create label {
    display: flex;
    gap: 4px;
    align-items: center;
    margin: 0;
}

.create label[hidden] {
    display: none;
}

.create input[type="checkbox"] {
    width: auto;
}

#join-error {
    color: var(--warn);
    margin: 4px 0;
    min-height: 1em;
}

.skin-row {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    align-items: center;
}

.skin-row label {
    margin: 0;
    color: var(--text);
    font-weight: 600;
}

.skin-row select,
#my-skin {
    padding: 4px 8px;
    border-radius: 6px;
    border: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.06);
    color: var(--text);
    font: inherit;
}

#my-skin {
    margin-bottom: 8px;
}

#game-skin-note {
    color: var(--muted);
}

#chat {
    position: fixed;
    left: max(10px, env(safe-area-inset-left));
    bottom: calc(max(16px, env(safe-area-inset-bottom)) + 110px);
    max-width: 45vw;
    font-size: 13px;
    text-shadow: 0 1px 2px #000;
    pointer-events: none;
}

#chat .channel {
    color: var(--muted);
}

#chat-menu {
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: min(420px, 92vw);
    max-height: 90vh;
    overflow: auto;
    padding: 10px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 10px;
    z-index: 8;
}

#chat-menu[hidden] {
    display: none;
}

#chat-channels,
#chat-macros {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 8px;
}

#chat-macros button,
#chat-channels button {
    padding: 6px 10px;
    border-radius: 6px;
    border: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.05);
    text-align: left;
}

#chat-macros button {
    flex: 1 1 45%;
}

#chat-channels button.chosen {
    border-color: var(--accent);
    background: rgba(122, 167, 255, 0.2);
}

.chat-hint {
    color: var(--muted);
    font-size: 11px;
}

#btn-chat {
    left: max(16px, env(safe-area-inset-left));
    top: calc(max(10px, env(safe-area-inset-top)) + 140px);
}

#scoreboard button.mute {
    border: 0;
    background: none;
    padding: 0 2px;
    font-size: 11px;
    cursor: pointer;
}

#scoreboard button.mute.on {
    opacity: 0.6;
}

#alliances {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

#alliances[hidden] {
    display: none;
}

.ally-title {
    font-weight: 600;
}

.ally-row {
    display: flex;
    justify-content: space-between;
    gap: 6px;
    align-items: center;
}

.ally-row button {
    padding: 2px 8px;
    border-radius: 6px;
    border: 1px solid var(--line);
    background: rgba(122, 167, 255, 0.2);
    font-size: 11px;
}

.ally-note {
    color: var(--muted);
}

#context {
    position: fixed;
    bottom: max(16px, env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%);
    font-size: 14px;
    text-shadow: 0 1px 3px #000;
    pointer-events: none;
    text-align: center;
}

#log {
    position: fixed;
    left: max(10px, env(safe-area-inset-left));
    bottom: max(16px, env(safe-area-inset-bottom));
    font-size: 12px;
    color: var(--muted);
    text-shadow: 0 1px 2px #000;
    pointer-events: none;
    max-width: 45vw;
}

#touch-buttons {
    display: none;
}

body.touch #touch-buttons {
    display: block;
}

#touch-buttons button {
    position: fixed;
    min-width: 56px;
    min-height: 56px;
    border-radius: 50%;
    border: 1px solid var(--line);
    background: rgba(20, 28, 48, 0.7);
    color: var(--text);
    font-size: 12px;
    touch-action: none;
}

#btn-shield {
    left: max(16px, env(safe-area-inset-left));
    bottom: calc(max(16px, env(safe-area-inset-bottom)) + 110px);
}

#btn-cloak {
    left: calc(max(16px, env(safe-area-inset-left)) + 68px);
    bottom: calc(max(16px, env(safe-area-inset-bottom)) + 110px);
    display: none;
}

body.can-cloak #btn-cloak {
    display: block;
}

#btn-shield.on,
#btn-cloak.on {
    background: rgba(122, 167, 255, 0.45);
}

#btn-beam {
    right: max(16px, env(safe-area-inset-right));
    bottom: calc(max(16px, env(safe-area-inset-bottom)) + 110px);
}

#btn-heavy {
    right: calc(max(16px, env(safe-area-inset-right)) + 68px);
    bottom: calc(max(16px, env(safe-area-inset-bottom)) + 110px);
    display: none;
}

body.has-heavy #btn-heavy {
    display: block;
}

#btn-pull {
    right: max(16px, env(safe-area-inset-right));
    bottom: calc(max(16px, env(safe-area-inset-bottom)) + 176px);
    min-width: 48px !important;
    min-height: 48px !important;
}

#btn-push {
    right: calc(max(16px, env(safe-area-inset-right)) + 60px);
    bottom: calc(max(16px, env(safe-area-inset-bottom)) + 176px);
    min-width: 48px !important;
    min-height: 48px !important;
}

#btn-map {
    right: calc(max(10px, env(safe-area-inset-right)) + 160px);
    top: max(10px, env(safe-area-inset-top));
}

#btn-home {
    left: 50%;
    bottom: calc(max(16px, env(safe-area-inset-bottom)) + 92px);
    transform: translateX(-50%);
    display: none;
    border-radius: 28px !important;
    padding: 0 16px;
}

body.touch.can-home #btn-home {
    display: block;
}

#btn-action {
    left: 50%;
    bottom: calc(max(16px, env(safe-area-inset-bottom)) + 28px);
    transform: translateX(-50%);
    display: none;
    border-radius: 28px !important;
    padding: 0 18px;
    background: rgba(88, 214, 141, 0.35) !important;
}

body.touch.docked #btn-action {
    display: block;
}

.overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: rgba(3, 5, 10, 0.75);
    z-index: 10;
}

.overlay[hidden] {
    display: none;
}

.overlay.small {
    background: transparent;
    pointer-events: none;
}

.overlay.small .panel {
    pointer-events: auto;
}

.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 18px 20px;
    max-width: 460px;
    width: 100%;
    max-height: 100%;
    overflow: auto;
    -webkit-user-select: text;
    user-select: text;
}

.panel h1 {
    margin: 0;
    font-size: 26px;
    letter-spacing: 0.02em;
}

.sub {
    margin: 4px 0 14px;
    color: var(--muted);
}

label {
    display: block;
    margin-bottom: 4px;
    color: var(--muted);
}

input {
    width: 100%;
    padding: 10px;
    border-radius: 6px;
    border: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.06);
    color: var(--text);
    font-size: 16px;
}

.row {
    display: flex;
    gap: 8px;
    margin: 12px 0;
}

.classes {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    margin: 12px 0;
}

.classes.compact .cls-info {
    display: none;
}

.classes.compact button.locked .cls-info {
    display: block;
}

.classes button.locked {
    opacity: 0.5;
    cursor: not-allowed;
}

#notice {
    position: fixed;
    top: calc(max(10px, env(safe-area-inset-top)) + 26px);
    left: 50%;
    transform: translateX(-50%);
    max-width: min(560px, 90vw);
    padding: 8px 12px;
    border-radius: 8px;
    background: rgba(231, 76, 60, 0.25);
    border: 1px solid rgba(231, 76, 60, 0.6);
    text-align: center;
    pointer-events: none;
}

.timer {
    color: var(--muted);
}

#scoreboard {
    display: none;
}

body.large-map #scoreboard {
    position: fixed;
    top: max(10px, env(safe-area-inset-top));
    left: max(10px, env(safe-area-inset-left));
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 8px;
    width: 200px;
    max-height: calc(100vh - 20px);
    overflow: auto;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    font-size: 11px;
    z-index: 5;
}

body.large-map #hud {
    display: none;
}

body.large-map #minimap {
    pointer-events: auto;
    cursor: pointer;
}

#scoreboard table {
    border-collapse: collapse;
    width: 100%;
}

#scoreboard caption {
    text-align: left;
    font-weight: 600;
}

#scoreboard th,
#scoreboard td {
    padding: 1px 4px;
    text-align: right;
}

#scoreboard th:first-child,
#scoreboard td:first-child {
    text-align: left;
}

#scoreboard tr.me td {
    color: #ffffff;
    font-weight: 600;
}

button {
    font: inherit;
    color: var(--text);
    cursor: pointer;
}

.classes button,
.row button {
    flex: 1;
    padding: 10px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.04);
    text-align: left;
}

.classes button.chosen,
.row button.chosen {
    border-color: var(--accent);
    background: rgba(122, 167, 255, 0.15);
}

.cls-name {
    display: block;
    font-weight: 600;
}

.cls-info {
    display: block;
    font-size: 12px;
    color: var(--muted);
}

#play,
#disconnected button {
    width: 100%;
    padding: 12px;
    border: 0;
    border-radius: 8px;
    background: var(--accent);
    color: #08101f;
    font-weight: 600;
    font-size: 16px;
}

details {
    margin-top: 12px;
    color: var(--muted);
    font-size: 13px;
}

summary {
    cursor: pointer;
    color: var(--text);
}

#rotate {
    display: none;
}

@media (orientation: portrait) {
    body.touch #rotate {
        display: flex;
    }
}

@media (max-height: 420px) {
    #minimap {
        width: 96px;
        height: 96px;
    }

    #btn-map {
        right: calc(max(10px, env(safe-area-inset-right)) + 104px);
    }

    #score {
        font-size: 11px;
        flex-wrap: wrap;
        justify-content: center;
        gap: 2px 10px;
        width: calc(100vw - 2 * 190px);
        white-space: normal;
        text-align: center;
    }

    .classes button {
        padding: 6px;
    }

    .cls-info {
        font-size: 11px;
    }

    #hud {
        width: 170px;
    }

    #stats {
        font-size: 11px;
    }

    .bar {
        height: 13px;
        margin-bottom: 2px;
    }

    .bar .label {
        line-height: 13px;
        font-size: 10px;
    }
}
