:root {
    --bg: #111417;
    --panel: #1b2024;
    --panel-2: #232a2f;
    --text: #edf1f4;
    --muted: #9eabb5;
    --line: #34404a;
    --accent: #f5b451;
    --accent-2: #4fb6a8;
    --danger: #f07167;
    --ok: #8ccf7e;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    font-family: Arial, Helvetica, sans-serif;
    color: var(--text);
    background: linear-gradient(180deg, #161a1e 0%, var(--bg) 42%, #0d1012 100%);
}

button,
input {
    font: inherit;
}

button {
    border: 1px solid #d4983f;
    border-radius: 6px;
    padding: 0.62rem 0.82rem;
    min-height: 42px;
    color: #17130c;
    background: var(--accent);
    cursor: pointer;
    font-weight: 700;
}

button:hover {
    filter: brightness(1.06);
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.55;
}

button.ghost {
    background: transparent;
    color: var(--text);
    border-color: var(--line);
}

input {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 0.72rem;
    color: var(--text);
    background: #11161a;
}

label {
    display: grid;
    gap: 0.38rem;
    color: var(--muted);
    font-size: 0.9rem;
}

.app-shell {
    width: min(1440px, 100%);
    margin: 0 auto;
    padding: 18px;
}

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

h1,
h2,
h3,
p {
    margin: 0;
}

h1 {
    font-size: clamp(1.7rem, 3vw, 2.8rem);
    letter-spacing: 0;
}

h2 {
    font-size: 1.05rem;
    margin-bottom: 14px;
}

h3 {
    font-size: 0.98rem;
}

.topbar p,
.muted,
.cost,
.rate,
.missing {
    color: var(--muted);
}

.top-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.badge {
    display: inline-flex;
    align-items: center;
    min-height: 34px;
    padding: 0 12px;
    border-radius: 999px;
    color: #111;
    background: var(--accent-2);
    font-weight: 800;
}

.auth-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 420px));
    gap: 18px;
    justify-content: center;
    margin-top: 8vh;
}

.panel {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 16px;
    background: color-mix(in srgb, var(--panel) 94%, black 6%);
}

.auth-grid form {
    display: grid;
    gap: 12px;
}

.plant-map {
    position: relative;
    height: 156px;
    margin: 4px 0 16px;
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
    background:
        linear-gradient(90deg, rgba(255,255,255,0.05) 1px, transparent 1px) 0 0 / 42px 42px,
        linear-gradient(180deg, rgba(255,255,255,0.04) 1px, transparent 1px) 0 0 / 42px 42px,
        radial-gradient(circle at 18% 82%, rgba(79, 182, 168, 0.14), transparent 22%),
        #151a1e;
}

.belt {
    position: absolute;
    left: 12%;
    right: 13%;
    height: 16px;
    border-top: 3px solid #7b8790;
    border-bottom: 3px solid #7b8790;
    background: repeating-linear-gradient(90deg, #2f383f 0 18px, #46515a 18px 28px);
}

.belt-a {
    top: 66px;
}

.belt-b {
    top: 96px;
    left: 27%;
    right: 24%;
    opacity: 0.78;
}

.machine-scene {
    position: absolute;
    top: 26px;
    width: 104px;
    height: 92px;
    display: grid;
    grid-template-rows: 1fr auto;
    place-items: center;
    gap: 4px;
    border: 1px solid #4c5862;
    border-radius: 6px;
    color: var(--text);
    background: #1b2227;
    font-weight: 800;
    font-size: 0.78rem;
    box-shadow: inset 0 -12px 0 rgba(0, 0, 0, 0.18);
}

.machine-scene.ore { left: 3%; }
.machine-scene.miner { left: 22%; }
.machine-scene.smelt { left: 41%; }
.machine-scene.build { right: 22%; }
.machine-scene.hub { right: 3%; }

.scene-icon,
.res-icon,
.machine-icon,
.research-icon {
    position: relative;
    display: inline-grid;
    place-items: center;
    flex: 0 0 auto;
}

.scene-icon {
    width: 58px;
    height: 52px;
}

.scene-icon::before,
.scene-icon::after,
.scene-icon i,
.res-icon::before,
.res-icon::after,
.res-icon i,
.machine-icon::before,
.machine-icon::after,
.machine-icon i,
.research-icon::before,
.research-icon::after,
.research-icon i {
    content: "";
    position: absolute;
    display: block;
}

.icon-ore::before {
    width: 48px;
    height: 34px;
    bottom: 4px;
    clip-path: polygon(12% 84%, 30% 28%, 56% 6%, 88% 38%, 76% 88%);
    background: linear-gradient(135deg, #8d99a4, #d1d6da);
}

.icon-ore::after {
    width: 13px;
    height: 13px;
    right: 14px;
    top: 18px;
    border-radius: 50%;
    background: #f08a4b;
}

.icon-miner::before,
.icon-smelter::before,
.icon-constructor::before,
.icon-hub::before {
    width: 50px;
    height: 34px;
    bottom: 5px;
    border-radius: 5px;
    background: #6d7780;
    box-shadow: inset 0 -8px 0 rgba(0, 0, 0, 0.24);
}

.icon-miner::after {
    width: 42px;
    height: 10px;
    top: 8px;
    transform: rotate(-20deg);
    background: #f5b451;
    border-radius: 4px;
}

.icon-smelter::before {
    background: #b85f3d;
}

.icon-smelter::after {
    width: 18px;
    height: 26px;
    top: 2px;
    border-radius: 10px 10px 4px 4px;
    background: linear-gradient(#ffdf7d, #f07167);
}

.icon-constructor::before {
    background: #4f9ab0;
}

.icon-constructor::after {
    width: 34px;
    height: 34px;
    top: 5px;
    border: 7px solid #d4dde3;
    border-radius: 50%;
}

.icon-hub::before {
    width: 54px;
    height: 44px;
    background: #d9a13f;
}

.icon-hub::after {
    width: 12px;
    height: 52px;
    right: 7px;
    top: 0;
    border-radius: 3px;
    background: #4fb6a8;
}

.metrics {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
    margin-bottom: 16px;
}

.metrics div {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px;
    background: var(--panel-2);
}

.metrics strong,
.metrics span {
    display: block;
}

.metrics strong {
    font-size: 1.25rem;
}

.metrics span {
    margin-top: 4px;
    color: var(--muted);
    font-size: 0.86rem;
}

.game-grid {
    display: grid;
    grid-template-columns: 1.05fr 1fr 1fr;
    gap: 14px;
}

.wide {
    grid-column: span 3;
}

.resource-grid,
.factory-grid,
.research-grid {
    display: grid;
    gap: 10px;
}

.resource-grid {
    grid-template-columns: repeat(auto-fit, minmax(135px, 1fr));
}

.resource {
    display: grid;
    grid-template-columns: 42px 1fr;
    align-items: center;
    gap: 10px;
    border: 1px solid #2d3840;
    border-radius: 6px;
    padding: 10px;
    background: #14191d;
}

.resource strong {
    display: block;
    font-size: 1.22rem;
}

.resource span {
    color: var(--muted);
    font-size: 0.84rem;
}

.action-list {
    display: grid;
    gap: 9px;
}

.action-row,
.factory,
.research {
    display: grid;
    gap: 8px;
    border: 1px solid #2d3840;
    border-radius: 6px;
    padding: 11px;
    background: #151b1f;
}

.action-row header,
.factory header,
.research header {
    display: flex;
    justify-content: space-between;
    gap: 12px;
}

.action-row h3,
.factory h3,
.research h3 {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.res-icon {
    width: 34px;
    height: 34px;
    border: 1px solid #41505a;
    border-radius: 6px;
    background: #101519;
}

.res-leaf::before {
    width: 20px;
    height: 25px;
    border-radius: 16px 2px 16px 2px;
    transform: rotate(28deg);
    background: #78b76d;
}

.res-wood::before {
    width: 24px;
    height: 14px;
    border-radius: 10px;
    background: #9b6b3d;
    box-shadow: inset 7px 0 0 #c09155;
}

.res-stone::before,
.res-ore::before,
.res-concrete::before {
    width: 24px;
    height: 20px;
    clip-path: polygon(10% 82%, 25% 20%, 62% 6%, 92% 44%, 75% 92%);
    background: #9da7ad;
}

.res-ore::after {
    width: 8px;
    height: 8px;
    right: 8px;
    top: 11px;
    border-radius: 50%;
    background: #f08a4b;
}

.res-copper::before {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: #c8794c;
    box-shadow: inset -5px -5px 0 rgba(0, 0, 0, 0.18);
}

.res-bio::before {
    width: 24px;
    height: 18px;
    border-radius: 50% 50% 6px 6px;
    background: #6ea85d;
}

.res-fuel::before {
    width: 16px;
    height: 24px;
    border-radius: 50% 50% 50% 50% / 65% 65% 35% 35%;
    background: linear-gradient(#ffd96c, #f07167);
}

.res-ingot::before,
.res-plate::before {
    width: 24px;
    height: 16px;
    border-radius: 3px;
    background: linear-gradient(135deg, #d7dde0, #7d8992);
}

.res-rod::before,
.res-pipe::before,
.res-wire::before,
.res-cable::before {
    width: 25px;
    height: 8px;
    border-radius: 8px;
    background: #c7d0d6;
}

.res-pipe::after {
    width: 14px;
    height: 14px;
    border: 4px solid #9aa7af;
    border-radius: 50%;
}

.res-screw::before {
    width: 24px;
    height: 8px;
    border-radius: 8px;
    transform: rotate(-24deg);
    background: repeating-linear-gradient(90deg, #d4dde3 0 5px, #89959d 5px 8px);
}

.res-wire::before,
.res-cable::before {
    background: #d98a51;
}

.res-cable::after {
    width: 25px;
    height: 8px;
    top: 18px;
    border-radius: 8px;
    background: #363f46;
}

.res-frame::before {
    width: 24px;
    height: 24px;
    border: 6px solid #aeb8bf;
    border-radius: 3px;
}

.res-rotor::before,
.res-motor::before {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: conic-gradient(#d4dde3 0 20%, transparent 20% 34%, #d4dde3 34% 54%, transparent 54% 68%, #d4dde3 68% 88%, transparent 88%);
}

.res-chip::before {
    width: 22px;
    height: 22px;
    border-radius: 3px;
    background: #4fb6a8;
    box-shadow: inset 0 0 0 5px #27454a;
}

.machine-icon {
    width: 38px;
    height: 34px;
    border-radius: 6px;
    background: #101519;
}

.machine-icon::before {
    width: 28px;
    height: 22px;
    bottom: 4px;
    border-radius: 4px;
    background: #68737b;
}

.machine-miner::after {
    width: 28px;
    height: 7px;
    top: 8px;
    transform: rotate(-22deg);
    background: var(--accent);
}

.machine-smelter::before,
.machine-burner::before {
    background: #a95a3c;
}

.machine-smelter::after,
.machine-burner::after {
    width: 10px;
    height: 18px;
    top: 6px;
    border-radius: 10px 10px 3px 3px;
    background: linear-gradient(#ffe08a, #f07167);
}

.machine-constructor::after,
.machine-assembler::after,
.machine-manufacturer::after {
    width: 18px;
    height: 18px;
    border: 5px solid #d4dde3;
    border-radius: 50%;
}

.machine-sawmill::after {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: repeating-conic-gradient(#d4dde3 0 18deg, transparent 18deg 36deg);
}

.research-icon {
    width: 32px;
    height: 32px;
    border: 1px solid #41505a;
    border-radius: 6px;
    background: #101519;
}

.research-icon::before {
    width: 18px;
    height: 22px;
    border-radius: 50% 50% 6px 6px;
    background: #f5b451;
}

.research-icon::after {
    width: 18px;
    height: 6px;
    bottom: 5px;
    border-radius: 2px;
    background: #4fb6a8;
}

.factory-grid {
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}

.research-grid {
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
}

.logic-tree {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 10px;
}

.logic-step {
    display: grid;
    grid-template-columns: 58px 1fr;
    gap: 10px;
    border: 1px solid #2d3840;
    border-radius: 6px;
    padding: 11px;
    background: #151b1f;
}

.logic-index {
    display: grid;
    place-items: center;
    min-height: 44px;
    border-radius: 6px;
    color: #111;
    background: var(--accent-2);
    font-weight: 800;
    font-size: 0.82rem;
    text-align: center;
}

.logic-step ul {
    margin: 8px 0 0;
    padding-left: 18px;
    color: var(--muted);
}

.logic-step li + li {
    margin-top: 4px;
}

.factory-actions {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
}

.state-on {
    color: var(--ok);
}

.state-off,
.missing {
    color: var(--danger);
}

.hub-box {
    display: grid;
    gap: 10px;
}

.toast {
    position: fixed;
    right: 18px;
    bottom: 18px;
    max-width: min(420px, calc(100vw - 36px));
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px 14px;
    background: #222a30;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
}

.hidden {
    display: none !important;
}

@media (max-width: 980px) {
    .auth-grid,
    .game-grid,
    .metrics {
        grid-template-columns: 1fr;
    }

    .wide {
        grid-column: auto;
    }

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

    .machine-scene {
        width: 86px;
    }

    .machine-scene.ore { left: 2%; }
    .machine-scene.miner { left: 21%; }
    .machine-scene.smelt { left: 40%; }
    .machine-scene.build { right: 21%; }
    .machine-scene.hub { right: 2%; }
}

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

    .plant-map {
        height: 132px;
    }

    .machine-scene {
        width: 62px;
        height: 78px;
        font-size: 0.7rem;
    }

    .scene-icon {
        width: 44px;
        height: 42px;
    }

    .belt-a {
        top: 58px;
    }

    .belt-b {
        top: 86px;
    }

    .factory-actions,
    .action-row header,
    .factory header,
    .research header {
        grid-template-columns: 1fr;
        display: grid;
    }
}
