:root {
    color-scheme: dark;
    --bg: #11131a;
    --panel: #1b1f2a;
    --panel-soft: #232938;
    --text: #f3f5f8;
    --muted: #aab2c0;
    --line: #303848;
    --accent: #f5b942;
    --good: #43c47a;
    --bad: #ff6474;
    --info: #63a6ff;
}

* { box-sizing: border-box; }
html, body {
    max-width: 100%;
    overflow-x: hidden;
}
body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    display: grid;
    grid-template-columns: 260px 1fr;
}

a { color: inherit; text-decoration: none; }
.sidebar {
    min-height: 100vh;
    border-right: 1px solid var(--line);
    background: #151923;
    padding: 22px;
    position: sticky;
    top: 0;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 18px; margin-bottom: 28px; }
.brand-mark { display: grid; place-items: center; width: 42px; height: 42px; background: var(--accent); color: #171717; border-radius: 8px; font-size: 13px; }
.brand-logo { width: 42px; height: 42px; object-fit: contain; border-radius: 8px; background: var(--panel-soft); }
nav { display: grid; gap: 8px; }
nav a, .logout {
    padding: 12px 14px;
    border-radius: 8px;
    color: var(--muted);
    background: transparent;
}
nav a:hover, .logout:hover { background: var(--panel-soft); color: var(--text); }
.logout { display: block; margin-top: 28px; color: var(--bad); }
.menu-toggle, .menu-scrim { display: none; }
.main { padding: 28px; min-width: 0; }
.page-head { display: flex; justify-content: space-between; gap: 18px; align-items: center; margin-bottom: 22px; }
h1, h2, p { margin-top: 0; }
h1 { font-size: 30px; margin-bottom: 6px; }
h2 { font-size: 18px; margin-bottom: 16px; }
p, small { color: var(--muted); }
.section-head { display: flex; justify-content: space-between; gap: 14px; align-items: flex-start; margin-bottom: 16px; }
.section-head h2 { margin-bottom: 4px; }
.button, button {
    border: 0;
    border-radius: 8px;
    padding: 11px 15px;
    background: var(--accent);
    color: #171717;
    font-weight: 800;
    cursor: pointer;
}
.button.muted { background: var(--panel-soft); color: var(--text); }
.button.danger { background: var(--bad); color: #fff; }
.panel, .auth-panel, .stats article {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 18px;
    min-width: 0;
}
.auth-panel { max-width: 420px; margin: 10vh auto; }
.alert {
    background: rgba(245, 185, 66, .14);
    border: 1px solid rgba(245, 185, 66, .35);
    color: #ffe2a3;
    padding: 12px 14px;
    border-radius: 8px;
    margin-bottom: 18px;
}
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; margin-bottom: 22px; }
.stats strong { display: block; font-size: 32px; }
.stats span { color: var(--muted); }
.server-panel { margin-bottom: 22px; }
.metric-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; }
.metric {
    background: #151923;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 14px;
}
.metric span { display: block; color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.metric strong { display: block; font-size: 26px; margin-bottom: 8px; }
.bar { height: 8px; background: #10141d; border-radius: 999px; overflow: hidden; margin-bottom: 8px; }
.bar i { display: block; height: 100%; min-width: 2px; max-width: 100%; background: var(--accent); }
.server-meta { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; color: var(--muted); font-size: 13px; }
.server-meta span { background: var(--panel-soft); border-radius: 999px; padding: 6px 9px; }
.grid.two { display: grid; grid-template-columns: minmax(320px, 420px) 1fr; gap: 18px; align-items: start; }
.tabs { display: flex; gap: 8px; margin-bottom: 18px; }
.tabs a {
    padding: 10px 13px;
    border: 1px solid var(--line);
    border-radius: 8px;
    color: var(--muted);
    background: var(--panel);
}
.tabs a.active { color: #171717; background: var(--accent); border-color: var(--accent); font-weight: 800; }
.form { display: grid; gap: 14px; }
label { display: grid; gap: 7px; color: var(--muted); font-size: 14px; }
input, select, textarea {
    width: 100%;
    background: #11151e;
    color: var(--text);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 11px 12px;
    font: inherit;
}
textarea { min-height: 86px; resize: vertical; }
input[type="file"] { padding: 9px 10px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.add-device-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}
.add-device-btn span {
    display: inline-grid;
    place-items: center;
    width: 22px;
    height: 22px;
    border-radius: 999px;
    background: rgba(245, 185, 66, .18);
    color: var(--accent);
    font-size: 17px;
    line-height: 1;
}
.device-group {
    display: grid;
    gap: 12px;
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #151923;
}
.device-group[hidden] { display: none; }
.device-group h3 {
    margin: 0;
    color: var(--text);
    font-size: 15px;
}
.device-group-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.device-group-head .icon-btn {
    margin-right: 0;
    width: 30px;
    height: 30px;
    color: var(--muted);
}
.expiry-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 12px; align-items: end; }
.check { display: flex; align-items: center; gap: 9px; }
.check input { width: auto; }
.switch-row {
    display: flex;
    flex-direction: row-reverse;
    justify-content: flex-end;
    gap: 10px;
    align-items: center;
    min-height: 43px;
    padding-bottom: 0;
    white-space: nowrap;
}
.switch-row span { color: var(--muted); }
.switch-row input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}
.switch-row i {
    display: block;
    flex: 0 0 auto;
    width: 54px;
    height: 30px;
    border-radius: 999px;
    background: #10141d;
    border: 1px solid var(--line);
    position: relative;
}
.switch-row i::before {
    content: "";
    position: absolute;
    width: 24px;
    height: 24px;
    top: 2px;
    left: 3px;
    border-radius: 50%;
    background: #f3f5f8;
    transition: transform .18s ease, background .18s ease;
}
.switch-row input:checked + i {
    background: rgba(67, 196, 122, .18);
    border-color: rgba(67, 196, 122, .5);
}
.switch-row input:checked + i::before {
    transform: translateX(23px);
    background: var(--good);
}
input:disabled { opacity: .55; cursor: not-allowed; }
table { width: 100%; border-collapse: collapse; }
th, td { border-bottom: 1px solid var(--line); padding: 12px 10px; text-align: left; vertical-align: top; }
th { color: var(--muted); font-size: 13px; font-weight: 700; }
td small { display: block; margin-top: 4px; word-break: break-all; }
.pill { display: inline-block; padding: 5px 9px; border-radius: 999px; background: var(--panel-soft); color: var(--muted); font-size: 12px; }
.pill.active { background: rgba(67, 196, 122, .16); color: var(--good); }
.pill.blocked { background: rgba(255, 100, 116, .16); color: var(--bad); }
.pill.paused { background: rgba(99, 166, 255, .16); color: var(--info); }
.actions { white-space: nowrap; }
.actions a { color: var(--accent); margin-right: 10px; }
.icon-btn {
    display: inline-grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border-radius: 8px;
    background: var(--panel-soft);
    border: 1px solid var(--line);
    color: var(--accent);
    margin-right: 7px;
    vertical-align: middle;
}
.icon-btn span { line-height: 1; font-size: 16px; }
.icon-btn:hover { border-color: var(--accent); }
.icon-btn.danger { color: var(--bad); }
.icon-btn.danger:hover { border-color: var(--bad); }
.icon-btn.warn { color: var(--info); }
.icon-btn.warn:hover { border-color: var(--info); }
.icon-btn.ok { color: var(--good); }
.icon-btn.ok:hover { border-color: var(--good); }
.mini-link {
    display: inline-block;
    margin: 0 6px 6px 0;
    padding: 5px 8px;
    border-radius: 999px;
    background: var(--panel-soft);
    color: var(--accent);
    font-size: 12px;
}
.template-list { display: grid; gap: 12px; }
.form-stack { display: grid; gap: 18px; }
.template-card {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #151923;
    padding: 14px;
}
.assigned-card .template-main { align-items: center; }
.template-main {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 12px;
    align-items: start;
}
.template-title { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 5px; }
.template-title strong { font-size: 16px; }
.template-card small { word-break: break-word; overflow-wrap: anywhere; }
.template-badges { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.template-footer {
    border-top: 1px solid var(--line);
    margin-top: 12px;
    padding-top: 12px;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 12px;
    align-items: center;
}
.vod-grid { display: grid; gap: 12px; }
.vod-card {
    display: grid;
    grid-template-columns: 100px minmax(0, 1fr);
    gap: 12px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #151923;
}
.vod-poster {
    width: 100px;
    height: 200px;
    border-radius: 8px;
    background: var(--panel-soft);
    overflow: hidden;
    display: grid;
    place-items: center;
    color: var(--muted);
    font-weight: 800;
}
.vod-poster img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.vod-info { min-width: 0; }
.vod-info small { word-break: break-word; overflow-wrap: anywhere; }
.vod-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}
.vod-actions { margin-top: 10px; white-space: normal; }
.vod-actions a { display: inline-block; margin-bottom: 6px; }
.assign-list { min-width: 0; }
.client-count-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}
.client-count-btn span {
    display: inline-grid;
    place-items: center;
    min-width: 24px;
    height: 24px;
    border-radius: 999px;
    background: rgba(245, 185, 66, .18);
    color: var(--accent);
    font-size: 12px;
}
.compact-btn {
    min-width: 0;
    padding: 8px 10px;
    white-space: nowrap;
}
.api-docs { margin-bottom: 18px; }
.api-docs code {
    display: block;
    margin-top: 10px;
    padding: 12px;
    border-radius: 8px;
    background: #10141d;
    color: #dbe7ff;
    word-break: break-all;
}
.channel-panel {
    grid-column: 1 / -1;
}
.channel-group-list {
    display: grid;
    gap: 10px;
    margin-top: 14px;
}
.channel-group {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #141923;
    overflow: hidden;
}
.channel-group summary {
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 14px;
    color: var(--text);
}
.channel-group summary span {
    color: var(--accent);
    font-weight: 800;
}
.channel-list {
    display: grid;
    gap: 8px;
    padding: 0 12px 12px;
}
.channel-row {
    border-top: 1px solid var(--line);
    padding-top: 10px;
}
.channel-edit-form {
    display: grid;
    grid-template-columns: 1.1fr .8fr .9fr 1.4fr auto;
    gap: 8px;
    align-items: end;
}
.channel-edit-form label {
    margin: 0;
}
.channel-edit-form input {
    min-height: 38px;
}
.channel-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    padding-bottom: 1px;
}
.channel-actions .compact-btn {
    min-height: 34px;
}
.empty-state {
    color: var(--muted);
    padding: 12px;
    border: 1px dashed var(--line);
    border-radius: 8px;
}
.system-preview {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 14px;
    border-radius: 8px;
    background: #151923;
    border: 1px solid var(--line);
}
.system-preview img { width: 72px; height: 72px; object-fit: contain; border-radius: 8px; background: var(--panel-soft); }
.system-preview strong { display: block; font-size: 20px; margin-bottom: 5px; }
.system-preview small { display: block; }
.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: grid;
    place-items: center;
    padding: 22px;
    background: rgba(0, 0, 0, .62);
    backdrop-filter: blur(4px);
}
.modal-backdrop[hidden] { display: none; }
.confirm-modal {
    width: min(440px, 100%);
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 22px;
    box-shadow: 0 22px 70px rgba(0, 0, 0, .45);
}
.confirm-modal h2 { margin-bottom: 8px; }
.confirm-modal p { margin-bottom: 20px; line-height: 1.45; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; }
.list-modal { width: min(560px, 100%); }
.modal-head {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    align-items: flex-start;
    margin-bottom: 18px;
}
.modal-head h2 { margin-bottom: 5px; }
.modal-head p { margin-bottom: 0; }
.modal-head .icon-btn {
    margin-right: 0;
    flex: 0 0 auto;
    color: var(--muted);
}
.client-modal-list {
    display: grid;
    gap: 10px;
    margin-bottom: 18px;
    max-height: min(420px, 58vh);
    overflow: auto;
}
.client-modal-list article {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 12px;
    align-items: center;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #151923;
}
.client-modal-list small {
    display: block;
    margin-top: 4px;
    word-break: break-all;
}
.playlist-list-modal { width: min(720px, 100%); }
.playlist-modal-list article {
    grid-template-columns: minmax(0, 1fr) auto;
}
.playlist-modal-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
}
.playlist-modal-actions .icon-btn { margin-right: 0; }
.device-edit-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: 18px;
}
.device-edit-grid section,
.link-list article {
    display: grid;
    gap: 12px;
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #151923;
}
.device-edit-grid h3 {
    margin: 0;
    font-size: 15px;
}
.links-modal { width: min(760px, 100%); }
.link-list {
    display: grid;
    gap: 12px;
    margin-bottom: 18px;
    max-height: min(500px, 62vh);
    overflow: auto;
}
.link-list small {
    display: block;
    word-break: break-all;
}
.link-list input {
    font-size: 13px;
}
.empty-state {
    padding: 16px;
    margin-bottom: 18px;
    border: 1px dashed var(--line);
    border-radius: 8px;
    color: var(--muted);
    background: #151923;
}

@media (max-width: 900px) {
    body {
        display: block;
        width: 100%;
        grid-template-columns: none;
    }
    body.menu-open { overflow: hidden; }
    .menu-toggle {
        position: fixed;
        top: 14px;
        right: 14px;
        z-index: 1200;
        display: grid;
        gap: 5px;
        place-content: center;
        width: 44px;
        height: 44px;
        padding: 0;
        background: var(--accent);
        border: 1px solid rgba(0, 0, 0, .18);
        box-shadow: 0 10px 28px rgba(0, 0, 0, .32);
    }
    .menu-toggle span {
        display: block;
        width: 20px;
        height: 2px;
        border-radius: 999px;
        background: #171717;
    }
    .menu-scrim {
        position: fixed;
        inset: 0;
        z-index: 1000;
        display: block;
        background: rgba(0, 0, 0, .58);
        backdrop-filter: blur(2px);
    }
    .menu-scrim[hidden] { display: none; }
    .sidebar {
        position: fixed;
        inset: 0 0 0 auto;
        z-index: 1100;
        width: min(78vw, 280px);
        min-height: 100vh;
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
        transition: opacity .16s ease, visibility .16s ease;
        box-shadow: -18px 0 50px rgba(0, 0, 0, .36);
        overflow-y: auto;
    }
    body.menu-open .sidebar {
        opacity: 1;
        visibility: visible;
        pointer-events: auto;
    }
    .main {
        width: 100%;
        max-width: 100vw;
        margin: 0;
        overflow-x: hidden;
        padding: 68px 8px 18px;
    }
    .main > * { max-width: 100%; }
    .panel,
    .form,
    .template-card,
    .device-group,
    .clients-table tr {
        max-width: 100%;
        overflow-x: hidden;
    }
    .grid.two > *,
    .stats > *,
    .metric-grid > * {
        min-width: 0;
        width: 100%;
    }
    .grid.two, .stats, .metric-grid { grid-template-columns: minmax(0, 1fr); }
    .grid.two { gap: 12px; }
    .panel { padding: 12px; }
    .page-head { margin-bottom: 14px; }
    h1 { font-size: 26px; margin-bottom: 4px; }
    h2 { font-size: 17px; margin-bottom: 12px; }
    p { margin-bottom: 12px; }
    .form { gap: 10px; }
    label { gap: 5px; font-size: 13px; }
    input, select, textarea { padding: 9px 10px; border-radius: 7px; }
    textarea { min-height: 62px; }
    .button, button { padding: 9px 12px; border-radius: 7px; }
    .add-device-btn { min-height: 38px; }
    .stats, .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-bottom: 12px; }
    .stats article { padding: 10px; }
    .stats strong { font-size: 24px; line-height: 1; }
    .stats span { font-size: 12px; }
    .server-panel { margin-bottom: 12px; }
    .section-head { gap: 8px; margin-bottom: 10px; }
    .section-head p { margin-bottom: 0; }
    .metric { padding: 10px; }
    .metric span { font-size: 11px; margin-bottom: 5px; }
    .metric strong { font-size: 20px; margin-bottom: 5px; }
    .metric small { font-size: 11px; }
    .bar { height: 6px; margin-bottom: 5px; }
    .server-meta { gap: 6px; margin-top: 10px; font-size: 11px; }
    .server-meta span { padding: 5px 7px; }
    table:not(.clients-table) {
        display: block;
        overflow-x: auto;
        font-size: 12px;
        white-space: nowrap;
    }
    table:not(.clients-table) th,
    table:not(.clients-table) td {
        padding: 8px 7px;
    }
    .template-list { gap: 8px; }
    .form-stack { gap: 12px; }
    .template-card { padding: 10px; }
    .vod-grid { gap: 8px; }
    .vod-card {
        grid-template-columns: 74px minmax(0, 1fr);
        gap: 10px;
        padding: 10px;
    }
    .vod-poster {
        width: 74px;
        height: 148px;
    }
    .template-main { gap: 8px; }
    .template-title { gap: 6px; margin-bottom: 3px; }
    .template-title strong { font-size: 15px; }
    .template-badges { gap: 5px; }
    .template-footer { margin-top: 8px; padding-top: 8px; gap: 8px; }
    .pill { padding: 4px 7px; font-size: 11px; }
    .compact-btn { padding: 7px 9px; }
    .api-docs { margin-bottom: 10px; }
    .api-docs code {
        margin-top: 7px;
        padding: 9px;
        font-size: 12px;
        line-height: 1.35;
    }
    .system-preview {
        gap: 10px;
        padding: 10px;
    }
    .system-preview img,
    .system-preview .brand-mark {
        width: 54px;
        height: 54px;
    }
    .system-preview strong { font-size: 17px; margin-bottom: 3px; }
    .alert { padding: 9px 10px; margin-bottom: 10px; }
    .clients-table,
    .clients-table tbody,
    .clients-table tr,
    .clients-table td {
        display: block;
        width: 100%;
    }
    .clients-table thead { display: none; }
    .clients-table tr {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px;
        padding: 12px;
        margin-bottom: 10px;
        border: 1px solid var(--line);
        border-radius: 8px;
        background: #151923;
    }
    .clients-table tr:last-child { margin-bottom: 0; }
    .clients-table td {
        width: auto;
        border-bottom: 0;
        padding: 0;
        min-width: 0;
    }
    .clients-table td::before {
        content: attr(data-label);
        display: block;
        margin-bottom: 4px;
        color: var(--muted);
        font-size: 11px;
        font-weight: 800;
    }
    .clients-table td:first-child {
        grid-column: 1 / -1;
        padding-top: 0;
        font-size: 16px;
    }
    .clients-table td:first-child::before { display: none; }
    .clients-table .actions {
        display: flex;
        gap: 8px;
        white-space: normal;
    }
    .clients-table .actions a { margin-right: 0; }
    .clients-table .compact-btn,
    .clients-table .client-count-btn {
        width: 100%;
        justify-content: center;
    }
    .clients-table .icon-btn {
        width: 32px;
        height: 32px;
    }
    .expiry-row { grid-template-columns: 1fr; gap: 8px; }
    .expiry-row .switch-row { justify-content: flex-start; }
    .switch-row { min-height: 38px; }
    .tabs { gap: 6px; margin-bottom: 12px; }
    .tabs a { padding: 8px 10px; }
    .template-main, .template-footer { grid-template-columns: 1fr; }
    .template-badges { justify-content: flex-start; }
    .page-head { align-items: flex-start; flex-direction: column; }
    .client-modal-list article, .playlist-modal-list article { grid-template-columns: 1fr; }
    .playlist-modal-actions { justify-content: flex-start; }
    .device-edit-grid { grid-template-columns: 1fr; }
    .channel-edit-form {
        grid-template-columns: 1fr;
    }
    .channel-actions {
        justify-content: flex-start;
        flex-wrap: wrap;
    }
}
