:root {
    --bg: #f4f6f8;
    --surface: #ffffff;
    --surface-alt: #eef2f5;
    --text: #17202a;
    --muted: #66717d;
    --border: #d8dee5;
    --primary: #1769aa;
    --primary-hover: #0f568e;
    --danger: #b42318;
    --danger-soft: #fee4e2;
    --success: #067647;
    --success-soft: #d1fadf;
    --info: #175cd3;
    --info-soft: #eaf2ff;
    --shadow: 0 12px 32px rgba(23, 32, 42, .08);
    --radius: 14px;
    --rack-row: 34px;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
    margin: 0;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--text);
    background: var(--bg);
    line-height: 1.5;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: none; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }

.topbar {
    min-height: 66px;
    display: flex;
    align-items: center;
    gap: 28px;
    padding: 0 28px;
    background: #101820;
    color: #fff;
    position: sticky;
    top: 0;
    z-index: 20;
    box-shadow: 0 3px 14px rgba(0,0,0,.18);
}
.brand { color: #fff; font-size: 1.25rem; font-weight: 800; letter-spacing: -.02em; white-space: nowrap; }
.brand span, .install-logo span { color: #60a5fa; }
.topbar nav { display: flex; align-self: stretch; align-items: stretch; gap: 4px; }
.topbar nav a { color: #c5ced8; display: flex; align-items: center; padding: 0 14px; border-bottom: 3px solid transparent; }
.topbar nav a:hover, .topbar nav a.active { color: #fff; border-bottom-color: #60a5fa; }
.user-menu { margin-left: auto; display: flex; align-items: center; gap: 14px; font-size: .9rem; }
.user-menu span { color: #fff; font-weight: 700; }
.user-menu a { color: #c5ced8; }

.container { width: min(1500px, calc(100% - 40px)); margin: 0 auto; padding: 30px 0 56px; }
.page-title { margin-bottom: 24px; }
.page-title h1 { margin: 0; font-size: clamp(1.7rem, 3vw, 2.35rem); letter-spacing: -.035em; }
h2 { margin: 0 0 8px; font-size: 1.18rem; }
h3 { margin: 0; }
p { margin-top: 0; }
.muted, .form-hint, small { color: var(--muted); }
.form-hint { font-size: .86rem; margin: 12px 0 0; }

.alert { border-radius: 10px; padding: 12px 14px; margin: 0 0 20px; border: 1px solid transparent; }
.alert-success { color: var(--success); background: var(--success-soft); border-color: #a6f4c5; }
.alert-error { color: var(--danger); background: var(--danger-soft); border-color: #fecdca; }
.alert-info { color: var(--info); background: var(--info-soft); border-color: #b2ccff; }

.stats-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 18px; margin-bottom: 34px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 23px; display: flex; flex-direction: column; }
.stat-card strong { font-size: 2.1rem; line-height: 1; margin-bottom: 8px; }
.stat-card span { color: var(--muted); }

.section-head { display: flex; align-items: end; justify-content: space-between; gap: 20px; margin: 26px 0 18px; }
.section-head p { color: var(--muted); margin: 0; }
.section-head.compact { margin-top: 0; align-items: center; }
.card-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 18px; }
.rack-card { display: block; background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; transition: transform .16s ease, border-color .16s ease; }
.rack-card:hover { transform: translateY(-2px); border-color: #8fbbe2; }
.rack-card-head { display: flex; justify-content: space-between; gap: 12px; }
.rack-card-head span { background: var(--surface-alt); border-radius: 999px; padding: 3px 9px; font-size: .82rem; }
.rack-card p { color: var(--muted); margin: 8px 0 16px; }
.progress { height: 8px; background: #e7ebef; border-radius: 99px; overflow: hidden; }
.progress span { display: block; height: 100%; background: var(--primary); }
.rack-meta { display: flex; justify-content: space-between; gap: 10px; color: var(--muted); font-size: .82rem; margin-top: 10px; }

.button { appearance: none; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 9px 14px; border-radius: 9px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; line-height: 1.2; }
.button:hover { border-color: #9da8b3; }
.button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.button.primary:hover { background: var(--primary-hover); }
.button.danger { color: var(--danger); border-color: #fecdca; background: #fff; }
.button.small { padding: 6px 9px; font-size: .78rem; }
.button.full { width: 100%; }
.button-row, .actions-cell { display: flex; gap: 7px; align-items: center; flex-wrap: wrap; }
.actions-cell { justify-content: flex-end; }
.actions-cell form { margin: 0; }
.icon-button { border: 0; background: rgba(255,255,255,.75); color: #111827; width: 22px; height: 22px; border-radius: 50%; font-size: 1rem; line-height: 1; padding: 0; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 22px; align-self: start; }
.split-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(310px, 380px); gap: 24px; align-items: start; }
.devices-layout { grid-template-columns: minmax(0, 1fr) minmax(340px, 430px); }
.stack-form { display: grid; gap: 14px; }
.stack-form label { display: grid; gap: 6px; font-weight: 700; font-size: .9rem; }
.stack-form small { font-weight: 400; }
.grid { display: grid; gap: 12px; }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
input, select, textarea {
    width: 100%; border: 1px solid #bdc6cf; border-radius: 8px; background: var(--surface); color: var(--text); padding: 10px 11px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(23,105,170,.15); }
textarea { resize: vertical; }
hr { border: 0; border-top: 1px solid var(--border); width: 100%; margin: 4px 0; }
.search-input { max-width: 260px; }
.inline-form { display: flex; gap: 7px; align-items: center; }
.inline-form input { width: 170px; padding: 7px 9px; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
table { border-collapse: collapse; width: 100%; min-width: 760px; }
th, td { padding: 13px 14px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { background: var(--surface-alt); color: #45515d; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fafbfc; }
.table-link { font-weight: 800; }
.badge { display: inline-block; margin-left: 6px; background: #e7eff8; color: #315c85; border-radius: 999px; padding: 2px 7px; font-size: .68rem; font-weight: 800; vertical-align: middle; }
.type-pill { display: inline-flex; border-radius: 999px; padding: 4px 9px; font-size: .74rem; font-weight: 800; background: #e8edf2; }

.empty-state { text-align: center; background: var(--surface); border: 1px dashed #aeb8c2; border-radius: var(--radius); padding: 52px 25px; }
.empty-state.small { padding: 30px; }

.rack-summary { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 20px; }
.rack-summary > div { background: var(--surface); border: 1px solid var(--border); border-radius: 11px; padding: 14px 16px; display: flex; flex-direction: column; }
.rack-summary span { color: var(--muted); font-size: .78rem; }
.rack-summary strong { font-size: 1rem; }
.rack-page-grid { display: grid; grid-template-columns: minmax(700px, 1fr) minmax(320px, 390px); gap: 22px; align-items: start; }
.rack-views { display: grid; grid-template-columns: repeat(2, minmax(330px, 1fr)); gap: 18px; }
.rack-view-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; min-width: 0; }
.rack-view-head { display: flex; justify-content: space-between; align-items: start; margin-bottom: 15px; }
.rack-view-head p { color: var(--muted); margin: 0; font-size: .84rem; }
.occupancy { border-radius: 999px; background: var(--surface-alt); padding: 4px 9px; font-size: .75rem; font-weight: 800; }
.rack-shell { display: grid; grid-template-columns: 35px minmax(0, 1fr); background: #242a30; padding: 8px; border: 5px solid #11161b; border-radius: 5px; overflow: hidden; }
.rack-numbers, .rack-stack { display: grid; grid-template-rows: repeat(var(--rack-units), var(--rack-row)); }
.rack-numbers span { display: flex; align-items: center; justify-content: center; color: #aeb8c2; border-bottom: 1px solid #444c54; font-size: .62rem; }
.rack-stack { position: relative; background: repeating-linear-gradient(to bottom, #d9dfe5 0, #d9dfe5 calc(var(--rack-row) - 1px), #aab4bd calc(var(--rack-row) - 1px), #aab4bd var(--rack-row)); border: 2px solid #0b0f13; }
.rack-device { z-index: 2; margin: 2px 4px; border: 1px solid rgba(0,0,0,.35); border-radius: 5px; padding: 4px 6px; display: flex; justify-content: space-between; gap: 6px; overflow: hidden; min-width: 0; background: #bfdbfe; color: #111827; box-shadow: inset 0 0 0 1px rgba(255,255,255,.3), 0 2px 5px rgba(0,0,0,.18); }
.rack-device-main { display: flex; flex-direction: column; min-width: 0; justify-content: center; }
.rack-device strong, .rack-device span, .rack-device small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rack-device strong { font-size: .72rem; }
.rack-device span { font-size: .58rem; }
.rack-device small { color: #273444; font-size: .58rem; }
.rack-device-meta { display: flex; flex-direction: column; align-items: flex-end; justify-content: space-between; flex: 0 0 auto; }
.rack-device-meta > span { font-size: .56rem; font-weight: 800; }
.rack-device.type-switch { background: #bbf7d0; }
.rack-device.type-router { background: #bfdbfe; }
.rack-device.type-firewall { background: #fecaca; }
.rack-device.type-patchpanel { background: #e5e7eb; }
.rack-device.type-server { background: #ddd6fe; }
.rack-device.type-storage { background: #fde68a; }
.rack-device.type-ups { background: #fed7aa; }
.rack-device.type-pdu { background: #cbd5e1; }
.rack-device.type-shelf { background: #d6d3d1; }
.rack-device.type-other { background: #cffafe; }
.rack-sidebar { display: grid; gap: 18px; }

.footer { color: var(--muted); text-align: center; font-size: .8rem; padding: 20px; }
.login-page, .install-page { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: radial-gradient(circle at top, #23435e, #0c151e 60%); }
.login-card, .install-card { width: min(100%, 440px); background: var(--surface); color: var(--text); border-radius: 18px; box-shadow: 0 24px 80px rgba(0,0,0,.35); padding: 30px; }
.install-card { width: min(100%, 600px); }
.install-logo { font-size: 1.45rem; font-weight: 900; margin-bottom: 18px; }
.login-card h1, .install-card h1 { margin: 0 0 18px; }

@media (max-width: 1150px) {
    .card-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .rack-page-grid { grid-template-columns: 1fr; }
    .rack-sidebar { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 850px) {
    .topbar { flex-wrap: wrap; padding: 12px 18px 0; gap: 8px 18px; position: static; }
    .topbar nav { order: 3; width: 100%; overflow-x: auto; }
    .topbar nav a { padding: 10px 12px; }
    .user-menu { margin-left: auto; }
    .container { width: min(100% - 24px, 1500px); padding-top: 22px; }
    .stats-grid, .rack-summary { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .split-layout, .devices-layout { grid-template-columns: 1fr; }
    .rack-views { grid-template-columns: 1fr; }
    .rack-sidebar { grid-template-columns: 1fr; }
}

@media (max-width: 560px) {
    .stats-grid, .rack-summary, .card-grid, .grid.two, .grid.three { grid-template-columns: 1fr; }
    .section-head { align-items: stretch; flex-direction: column; }
    .search-input { max-width: none; }
    .rack-meta { flex-direction: column; }
    .login-card, .install-card { padding: 22px; }
}

@media print {
    .topbar, .footer, .rack-sidebar, .page-title, .alert { display: none !important; }
    body, .container { background: #fff; width: 100%; margin: 0; padding: 0; }
    .rack-page-grid { display: block; }
    .rack-view-card { box-shadow: none; break-inside: avoid; margin-bottom: 18px; }
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #10151b;
        --surface: #182029;
        --surface-alt: #222c36;
        --text: #edf2f7;
        --muted: #a8b2bd;
        --border: #35414c;
        --shadow: 0 12px 32px rgba(0,0,0,.25);
    }
    input, select, textarea { border-color: #4b5864; }
    tbody tr:hover { background: #1d2730; }
    .button.danger { background: var(--surface); }
    .progress { background: #303b45; }
    .badge { background: #243e55; color: #b7d6f2; }
}
