:root {
    --bg: #f4f6fb;
    --panel: #ffffff;
    --text: #172033;
    --muted: #64728a;
    --line: #dfe5ef;
    --primary: #2454d8;
    --primary-dark: #173ca2;
    --danger: #c43232;
    --success: #167a3a;
    --warning: #9a6500;
    --radius: 16px;
    --shadow: 0 18px 50px rgba(19, 31, 58, 0.08);
}

* { box-sizing: border-box; }
body {
    margin: 0;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--text);
    background: var(--bg);
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
code { background: #eef2fa; padding: .12rem .35rem; border-radius: 6px; }

.login-page, .setup-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
}
.login-card, .setup-card {
    width: min(620px, 100%);
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 32px;
}
.setup-card { width: min(820px, 100%); }
h1, h2, h3 { margin: 0 0 12px; }
p { line-height: 1.55; }
.muted, .hint { color: var(--muted); }
.hint { font-size: .9rem; margin: 6px 0 0; }

.topbar {
    height: 64px;
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    background: rgba(255,255,255,.92);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--line);
}
.pill {
    display: inline-flex;
    padding: 3px 8px;
    border-radius: 999px;
    background: #eef2fa;
    color: var(--muted);
    font-size: .8rem;
    margin-left: 8px;
}
.layout { display: grid; grid-template-columns: 250px 1fr; min-height: calc(100vh - 64px); }
.sidebar {
    border-right: 1px solid var(--line);
    background: #fff;
    padding: 18px;
    position: sticky;
    top: 64px;
    height: calc(100vh - 64px);
}
.nav-btn {
    width: 100%;
    display: block;
    text-align: left;
    border: 0;
    background: transparent;
    padding: 12px 14px;
    border-radius: 12px;
    font: inherit;
    cursor: pointer;
    color: var(--text);
}
.nav-btn:hover { background: #f1f4fb; }
.nav-btn.active { background: var(--primary); color: #fff; }
.content { padding: 28px; min-width: 0; }
.tab { display: none; }
.tab.active { display: block; }
.page-head {
    display: flex;
    justify-content: space-between;
    gap: 20px;
    align-items: flex-start;
    margin-bottom: 18px;
}
.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
    margin-bottom: 20px;
}
.cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 14px;
    margin-bottom: 20px;
}
.card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 18px;
    box-shadow: var(--shadow);
    min-width: 0;
    overflow: hidden;
}
.card .value { font-size: 2rem; font-weight: 700; overflow-wrap: anywhere; line-height: 1.15; }
.card .value.compact { font-size: 1.16rem; line-height: 1.25; }
.card .label { color: var(--muted); }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.form-stack { display: grid; gap: 14px; }
label { display: grid; gap: 7px; font-weight: 600; }
input, textarea, select {
    width: 100%;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--text);
    border-radius: 11px;
    padding: 10px 12px;
    font: inherit;
}
textarea { resize: vertical; }
select[multiple] { min-height: 190px; }
.checkline {
    display: flex;
    align-items: center;
    gap: 9px;
    font-weight: 500;
}
.checkline input { width: auto; }
.top-space { padding-top: 28px; }
.button-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--text);
    border-radius: 11px;
    padding: 10px 14px;
    font: inherit;
    font-weight: 650;
    cursor: pointer;
    min-height: 42px;
}
.button:hover { background: #f3f6fb; text-decoration: none; }
.button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.button.primary:hover { background: var(--primary-dark); }
.button.danger { color: #fff; background: var(--danger); border-color: var(--danger); }
.notice {
    border-radius: 12px;
    padding: 12px 14px;
    margin: 12px 0;
    border: 1px solid var(--line);
}
.notice.error { background: #fff0f0; border-color: #ffd0d0; color: #842121; }
.notice.success { background: #ecfff2; border-color: #bceacb; color: #0d5a2b; }
.notice.neutral { background: #f6f8fc; color: var(--muted); }
.check-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); margin-bottom: 16px; }
.check {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: 12px;
    border: 1px solid var(--line);
    background: #fff;
}
.check span { width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 700; }
.check.ok span { background: var(--success); }
.check.bad span { background: var(--warning); }
.copy-row { display: flex; gap: 10px; }
.copy-row input { flex: 1; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.output {
    background: #0f172a;
    color: #dbeafe;
    border-radius: 12px;
    padding: 14px;
    overflow: auto;
    max-height: 360px;
}
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .94rem; }
th, td { padding: 10px 9px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { color: var(--muted); font-weight: 700; background: #f8fafe; }
.list { display: grid; gap: 10px; }
.list-item {
    border: 1px solid var(--line);
    border-radius: 13px;
    padding: 12px;
    background: #fff;
    display: grid;
    gap: 6px;
}
.list-item .item-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.list-item small { color: var(--muted); word-break: break-word; }
.status-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; background: #bbb; }
.status-dot.on { background: var(--success); }
.status-dot.off { background: #a3a3a3; }
.checkbox-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 8px;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px;
    background: #fff;
}
.checkbox-grid label { display: flex; gap: 8px; align-items: center; font-weight: 500; }
.checkbox-grid input { width: auto; }
.filter-layout { display: grid; grid-template-columns: 280px 1fr; gap: 18px; align-items: start; }
.filter-list-panel { position: sticky; top: 84px; }
.map-block { border: 1px solid var(--line); border-radius: 16px; padding: 14px; background: #fbfcff; }
.map-head { display: flex; justify-content: space-between; align-items: start; gap: 18px; }
.map-head label { max-width: 230px; }
.map-toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
#filterMap { height: 420px; border-radius: 14px; border: 1px solid var(--line); background: #eaf0f8; }
.template-details { border: 1px solid var(--line); border-radius: 12px; padding: 12px; background: #fbfcff; }
.template-details summary { cursor: pointer; font-weight: 700; }
.template-details[open] { display: grid; gap: 12px; }
.preview { margin-top: 16px; }
.toast {
    position: fixed;
    right: 24px;
    bottom: 24px;
    max-width: 420px;
    background: #101827;
    color: #fff;
    border-radius: 13px;
    padding: 13px 16px;
    box-shadow: var(--shadow);
    z-index: 1000;
}
.badge { display: inline-block; padding: 2px 7px; border-radius: 999px; background: #eef2fa; color: var(--muted); font-size: .78rem; }
.badge.ok { background: #e7faee; color: var(--success); }
.badge.error { background: #ffecec; color: var(--danger); }
.template-preview-panel {
    border: 1px solid var(--line);
    border-radius: 14px;
    background: #fbfcff;
    padding: 14px;
    display: grid;
    gap: 12px;
}
.template-preview-output { display: grid; gap: 14px; }
.template-preview-output textarea[readonly] {
    background: #f8fafe;
    color: var(--text);
    min-height: 0;
}
.preview-rendered { align-items: start; }
.placeholder-table table { table-layout: fixed; }
.placeholder-table th:nth-child(1), .placeholder-table td:nth-child(1) { width: 170px; }
.placeholder-table th:nth-child(2), .placeholder-table td:nth-child(2) { width: 260px; }
.placeholder-value {
    margin: 0;
    max-height: 170px;
    overflow: auto;
    white-space: pre-wrap;
    word-break: break-word;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: .84rem;
    line-height: 1.38;
    background: #f3f6fb;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 9px;
}

@media (max-width: 1000px) {
    .layout { grid-template-columns: 1fr; }
    .sidebar { position: static; height: auto; display: flex; gap: 8px; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--line); }
    .nav-btn { white-space: nowrap; width: auto; }
    .filter-layout, .grid.two { grid-template-columns: 1fr; }
    .filter-list-panel { position: static; }
    .page-head, .map-head { flex-direction: column; }
}
