:root {
    color-scheme: dark;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
    background: #0b101b;
    color: #f2f4f7;
    font-synthesis: none;
    --surface: #151c2b;
    --surface-raised: #1b2435;
    --border: #293448;
    --muted: #98a2b3;
    --primary: #ff6b35;
    --primary-dark: #d94b1c;
    --green: #32d583;
    --red: #f97066;
    --blue: #53b1fd;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
    margin: 0;
    min-width: 320px;
    min-height: 100vh;
    background:
        radial-gradient(circle at 85% -10%, rgba(255, 107, 53, 0.16), transparent 32rem),
        #0b101b;
}

button, input, select { font: inherit; }
button { touch-action: manipulation; }

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: max(20px, env(safe-area-inset-top)) 18px 14px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: 24px; letter-spacing: -0.03em; }
h2 { margin-bottom: 0; font-size: 22px; }
h3 { margin-bottom: 0; font-size: 17px; }

.eyebrow {
    margin-bottom: 4px;
    color: var(--primary);
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 0.14em;
}

main {
    width: min(100%, 1120px);
    margin: 0 auto;
    padding: 16px 14px calc(92px + env(safe-area-inset-bottom));
}

.panel, .metric-card, .fan-card {
    border: 1px solid var(--border);
    border-radius: 18px;
    background: linear-gradient(145deg, rgba(27, 36, 53, 0.96), rgba(18, 25, 39, 0.96));
    box-shadow: 0 14px 35px rgba(0, 0, 0, 0.15);
}

.panel { padding: 18px; }
.auth-panel { margin-bottom: 24px; }
.auth-panel h2 { font-size: 17px; }
.auth-panel > div:first-child { margin-bottom: 12px; }
.auth-row { display: flex; align-items: end; gap: 10px; }
.auth-actions { display: flex; gap: 8px; }
.grow { flex: 1; }

label { display: grid; gap: 7px; color: #d0d5dd; font-size: 13px; }
.field-help {
    min-height: 34px;
    color: var(--muted);
    font-size: 11px;
    font-weight: 400;
    line-height: 1.55;
}
input, select {
    width: 100%;
    min-height: 46px;
    padding: 10px 12px;
    border: 1px solid #344054;
    border-radius: 11px;
    outline: none;
    background: #0e1522;
    color: #f9fafb;
}
input:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(255, 107, 53, 0.13); }

button {
    min-height: 46px;
    padding: 10px 16px;
    border: 0;
    border-radius: 11px;
    background: var(--primary);
    color: #fff;
    font-weight: 750;
    cursor: pointer;
}
button:hover { background: var(--primary-dark); }
button:disabled { cursor: wait; opacity: 0.6; }
button.secondary { border: 1px solid #3b485e; background: #202a3c; color: #e4e7ec; }
button.danger { background: rgba(249, 112, 102, 0.13); color: #fda29b; }
button.compact { min-height: 38px; padding: 7px 12px; font-size: 13px; }

.hint { margin-bottom: 0; color: var(--muted); font-size: 12px; line-height: 1.55; }
.message { min-height: 18px; margin: 10px 0 0; color: var(--muted); font-size: 13px; }
.message.success { color: var(--green); }
.message.error { color: var(--red); }
.badge { padding: 6px 9px; border-radius: 999px; background: rgba(50, 213, 131, 0.12); color: var(--green); font-size: 12px; font-weight: 700; white-space: nowrap; }
.badge.muted { background: #202939; color: var(--muted); }
.badge.offline { background: rgba(249, 112, 102, 0.12); color: var(--red); }

.view { display: none; }
.view.active { display: block; animation: appear 180ms ease-out; }
@keyframes appear { from { opacity: 0; transform: translateY(4px); } }

.section-heading, .panel-heading, .legend-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.section-heading { margin: 0 2px 14px; }
.section-heading .eyebrow { margin-bottom: 3px; }
.panel-heading { margin-bottom: 16px; }

.metric-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    margin-bottom: 12px;
}
.metric-card { min-height: 104px; padding: 16px; }
.metric-card span { display: block; margin-bottom: 11px; color: var(--muted); font-size: 12px; }
.metric-card strong { display: block; overflow: hidden; font-size: 24px; letter-spacing: -0.03em; text-overflow: ellipsis; }
.metric-card small { display: block; margin-top: 5px; color: var(--muted); }

.device-summary-row {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 4px 12px;
    padding: 13px 0;
    border-top: 1px solid var(--border);
}
.device-summary-row:first-child { padding-top: 0; border-top: 0; }
.device-summary-row strong { font-size: 14px; }
.device-summary-row small { color: var(--muted); }
.device-summary-row .status { grid-row: span 2; align-self: center; }

.device-grid, .settings-form { display: grid; gap: 14px; }
.device-card { overflow: hidden; }
.device-card-header { padding: 18px; }
.device-title { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.device-id { overflow: hidden; font-size: 17px; text-overflow: ellipsis; }
.device-meta { display: flex; flex-wrap: wrap; gap: 7px 14px; margin-top: 12px; color: var(--muted); font-size: 12px; }
.fan-list { display: grid; gap: 1px; background: var(--border); }
.fan-card { padding: 17px; border: 0; border-radius: 0; box-shadow: none; }
.fan-reading { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 14px 0; }
.fan-reading div { padding: 10px 6px; border-radius: 10px; background: #111927; text-align: center; }
.fan-reading strong, .fan-reading span { display: block; }
.fan-reading strong { margin-bottom: 3px; font-size: 17px; }
.fan-reading span { color: var(--muted); font-size: 10px; }
.control-row { display: grid; grid-template-columns: 105px 1fr; gap: 10px; align-items: end; }
.range-wrap { display: grid; grid-template-columns: 1fr 42px; gap: 9px; align-items: center; }
input[type="range"] { min-height: 38px; padding: 0; accent-color: var(--primary); box-shadow: none; }
.control-save { width: 100%; margin-top: 12px; }

fieldset { min-width: 0; margin: 0; }
fieldset.panel { padding-top: 14px; }
legend { padding: 0 5px; color: #f9fafb; font-size: 16px; font-weight: 750; }
.legend-row legend { padding: 0; }
.form-grid { display: grid; gap: 14px; }
#autoParameters { margin-top: 14px; }
.quiet-schedule { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--border); }
.checkbox-label { display: flex; align-items: center; gap: 9px; margin-bottom: 14px; }
.checkbox-label input { width: 20px; min-height: 20px; accent-color: var(--primary); }
.weather-details {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1px;
    overflow: hidden;
    margin-top: 16px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--border);
}
.weather-details div { min-width: 0; padding: 11px 12px; background: #111927; }
.weather-details span, .weather-details strong { display: block; }
.weather-details span { margin-bottom: 4px; color: var(--muted); font-size: 11px; }
.weather-details strong { overflow-wrap: anywhere; font-size: 13px; }
.weather-details .weather-error { color: var(--red); }
.danger-zone { border-color: rgba(249, 112, 102, 0.45); }
.danger-zone .danger { width: 100%; margin-top: 14px; }
.wifi-list { display: grid; gap: 10px; margin-top: 14px; }
.wifi-row { display: grid; grid-template-columns: 1fr 1fr auto; gap: 8px; align-items: end; }
.wifi-row .danger { width: 46px; padding: 0; }
.sticky-actions { position: sticky; bottom: calc(72px + env(safe-area-inset-bottom)); z-index: 5; padding: 12px; border: 1px solid var(--border); border-radius: 16px; background: rgba(14, 21, 34, 0.93); backdrop-filter: blur(16px); }
.sticky-actions .message { margin-top: 0; }
.sticky-actions button { width: 100%; }
.empty-state { padding: 24px; color: var(--muted); text-align: center; }

.bottom-nav {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 20;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    background: rgba(12, 17, 28, 0.92);
    backdrop-filter: blur(18px);
}
.nav-item { min-height: 48px; padding: 8px; background: transparent; color: var(--muted); font-size: 13px; }
.nav-item:hover { background: transparent; }
.nav-item.active { background: rgba(255, 107, 53, 0.12); color: #ff9b73; }

@media (min-width: 700px) {
    .topbar { padding-right: 28px; padding-left: 28px; }
    main { padding: 24px 24px 110px; }
    .metric-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
    .device-grid { grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); }
    .two-columns { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .wide { grid-column: 1 / -1; }
    .settings-form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .settings-form .sticky-actions { grid-column: 1 / -1; }
    .bottom-nav { right: auto; left: 50%; width: 420px; transform: translateX(-50%); border: 1px solid var(--border); border-bottom: 0; border-radius: 18px 18px 0 0; }
}

@media (max-width: 430px) {
    .auth-row { display: grid; }
    .auth-actions button { width: 100%; }
    .wifi-row { grid-template-columns: 1fr 46px; }
    .wifi-row label:nth-child(2) { grid-column: 1 / -1; grid-row: 2; }
    .wifi-row .danger { grid-column: 2; grid-row: 1; }
    .field-help { min-height: 0; }
    .weather-details { grid-template-columns: 1fr; }
}
