/* ─── Base ─────────────────────────────────────────────────────────────── */

html, body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    color: #222;
    background: #f7f8fa;
    -webkit-text-size-adjust: 100%; /* prevent iOS font-size autoboost */
}

.layout {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.content {
    flex: 1;
    padding: 24px 24px 48px;
    max-width: 1100px;
    width: 100%;
    box-sizing: border-box;
    margin: 0 auto;
}

h1 { margin-top: 0; }
h2 { margin-top: 32px; }

@media (max-width: 720px) {
    .content { padding: 16px 12px 32px; }
    h2 { margin-top: 24px; }
}

/* ─── Top navbar ───────────────────────────────────────────────────────── */

.nav-menu {
    background: #1f2937;
    color: #e5e7eb;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: 0 1px 0 rgba(0,0,0,.15);
}

.nav-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 56px;
    padding: 0 24px;
    max-width: 1100px;
    margin: 0 auto;
}

.brand a {
    color: #fff;
    font-size: 1.15em;
    font-weight: 600;
    text-decoration: none;
}

/* Hidden checkbox controls the mobile hamburger via :checked ~ selector. */
.nav-toggle-cb { display: none; }

/* Hamburger button — three lines that turn into an X when checked. */
.nav-toggle-btn {
    display: none;
    cursor: pointer;
    width: 30px;
    height: 30px;
    position: relative;
}
.nav-toggle-btn span {
    position: absolute;
    left: 4px;
    right: 4px;
    height: 2px;
    background: #e5e7eb;
    border-radius: 1px;
    transition: transform .2s, opacity .2s, top .2s;
}
.nav-toggle-btn span:nth-child(1) { top: 8px; }
.nav-toggle-btn span:nth-child(2) { top: 14px; }
.nav-toggle-btn span:nth-child(3) { top: 20px; }

.nav-toggle-cb:checked ~ .nav-bar .nav-toggle-btn span:nth-child(1) {
    top: 14px; transform: rotate(45deg);
}
.nav-toggle-cb:checked ~ .nav-bar .nav-toggle-btn span:nth-child(2) {
    opacity: 0;
}
.nav-toggle-cb:checked ~ .nav-bar .nav-toggle-btn span:nth-child(3) {
    top: 14px; transform: rotate(-45deg);
}

/* Links + sign-out container. Inline on desktop, collapsible on mobile. */
.nav-collapse {
    display: flex;
    align-items: center;
    padding: 0 24px;
    max-width: 1100px;
    margin: -56px auto 0; /* overlap into the nav-bar row on desktop */
    position: relative;
    height: 56px;
    pointer-events: none; /* let the brand still receive clicks */
}
.nav-collapse > * { pointer-events: auto; }

.nav-collapse ul {
    list-style: none;
    padding: 0;
    margin: 0 0 0 140px; /* leave room for brand on the left */
    display: flex;
    gap: 4px;
}
.nav-collapse ul a {
    display: block;
    padding: 8px 14px;
    color: #d1d5db;
    text-decoration: none;
    border-radius: 6px;
    font-size: 0.95em;
}
.nav-collapse ul a:hover { background: #374151; color: #fff; }
.nav-collapse ul a.active { background: #2563eb; color: #fff; }

.logout-form { margin: 0 0 0 auto; }
.logout-btn {
    background: transparent;
    color: #d1d5db;
    border: 1px solid #374151;
    border-radius: 6px;
    cursor: pointer;
    padding: 6px 14px;
    font-size: 0.9em;
}
.logout-btn:hover { background: #374151; color: #fff; }

/* ─── Mobile navbar ────────────────────────────────────────────────────── */

@media (max-width: 720px) {
    .nav-bar { padding: 0 16px; }
    .nav-toggle-btn { display: block; }

    .nav-collapse {
        display: none;
        position: static;
        margin: 0;
        height: auto;
        padding: 8px 16px 16px;
        background: #1f2937;
        flex-direction: column;
        align-items: stretch;
    }
    .nav-collapse ul {
        flex-direction: column;
        margin: 0;
        gap: 2px;
    }
    .nav-collapse ul a {
        padding: 12px 14px;
        font-size: 1em;
    }
    .logout-form { margin: 12px 0 0; }
    .logout-btn { width: 100%; padding: 12px; font-size: 1em; }

    .nav-toggle-cb:checked ~ .nav-collapse { display: flex; }
}

/* ─── Stats / cards ────────────────────────────────────────────────────── */

.subtitle { color: #6b7280; margin-top: -8px; font-size: 1.1em; }

.stat-row {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    margin: 16px 0 24px;
}

.stat {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: 14px 18px;
    flex: 1 1 140px;
    min-width: 140px;
}

.stat-label { color: #6b7280; font-size: 0.85em; }
.stat-value { font-size: 1.4em; font-weight: 600; margin-top: 4px; word-break: break-word; }

@media (max-width: 720px) {
    .stat { flex: 1 1 calc(50% - 6px); padding: 10px 12px; }
    .stat-value { font-size: 1.15em; }
}

/* ─── Tables ───────────────────────────────────────────────────────────── */

.table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    overflow: hidden;
}

.table th, .table td {
    padding: 10px 12px;
    text-align: left;
    border-bottom: 1px solid #f1f5f9;
}

.table th {
    background: #f9fafb;
    font-weight: 600;
    font-size: 0.9em;
    color: #374151;
}

.table tr:last-child td { border-bottom: none; }

/* Tables get wide easily (Channels, Recent readings). On narrow viewports
   wrap each in a horizontal-scroll surrogate so the rest of the layout stays
   intact. Using display:block hands width control to overflow-x.            */
@media (max-width: 720px) {
    .table {
        display: block;
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
    }
}

/* ─── Buttons ──────────────────────────────────────────────────────────── */

.btn {
    display: inline-block;
    padding: 8px 16px;
    background: #fff;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    color: #111827;
    text-decoration: none;
    cursor: pointer;
    font-size: 0.95em;
}
.btn:hover { background: #f3f4f6; }
.btn.primary {
    background: #2563eb;
    border-color: #2563eb;
    color: #fff;
}
.btn.primary:hover { background: #1d4ed8; }
.btn.small { padding: 4px 10px; font-size: 0.85em; }

@media (max-width: 720px) {
    .btn { padding: 10px 14px; }   /* finger-friendly */
    .btn.small { padding: 6px 10px; }
}

/* ─── Forms ────────────────────────────────────────────────────────────── */

.form-row {
    margin: 12px 0;
    max-width: 480px;
}
.form-row label {
    display: block;
    font-size: 0.9em;
    font-weight: 600;
    margin-bottom: 4px;
}
.form-row input,
.form-row textarea,
.form-row select {
    width: 100%;
    padding: 10px;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    box-sizing: border-box;
    font-family: inherit;
    font-size: 1em;
}
.form-row small {
    display: block;
    color: #6b7280;
    font-size: 0.8em;
    margin-top: 2px;
}

.error { color: #b91c1c; }

/* ─── Token / hint / misc ──────────────────────────────────────────────── */

.token-box {
    background: #fef3c7;
    border: 1px solid #fcd34d;
    border-radius: 8px;
    padding: 16px;
    margin: 16px 0;
}
.token-box pre {
    background: #fff;
    padding: 12px;
    border-radius: 6px;
    overflow-x: auto;
    font-size: 1em;
    margin: 8px 0;
}
.token-box .hint { color: #78350f; font-size: 0.9em; }

code {
    background: #f1f5f9;
    padding: 2px 6px;
    border-radius: 4px;
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    font-size: 0.92em;
    word-break: break-all;
}

.hint { color: #6b7280; font-size: 0.9em; }

h1:focus { outline: none; }
.valid.modified:not([type=checkbox]) { outline: 1px solid #26b050; }
.invalid { outline: 1px solid #e50000; }
.validation-message { color: #e50000; }

/* ─── Range buttons + series toggles ───────────────────────────────────── */

.range-buttons {
    display: flex;
    gap: 8px;
    margin: 12px 0;
    flex-wrap: wrap;
}

.series-toggles {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin: 8px 0 16px;
}

.toggle-group {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 14px;
    padding: 8px 14px;
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
}

.toggle-label {
    font-weight: 600;
    color: #6b7280;
    font-size: 0.8em;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.toggle {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    font-size: 0.95em;
    user-select: none;
}
.toggle input { margin: 0; cursor: pointer; }
.toggle:hover { color: #2563eb; }

/* ─── Chart ────────────────────────────────────────────────────────────── */

.chart-wrap {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: 16px;
    height: 380px;
    position: relative;
    margin-bottom: 24px;
}

@media (max-width: 720px) {
    .chart-wrap { padding: 8px; height: 280px; }
}

/* ─── Login card ───────────────────────────────────────────────────────── */

.login-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f7f8fa;
    padding: 16px;
}

.login-card {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    padding: 32px;
    width: 100%;
    max-width: 380px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.login-card h1 {
    margin: 0 0 4px;
    font-size: 1.5em;
}

.login-card .subtitle {
    margin: 0 0 24px;
    color: #6b7280;
}

.error-banner {
    background: #fee2e2;
    border: 1px solid #fca5a5;
    color: #991b1b;
    padding: 10px 14px;
    border-radius: 6px;
    margin-bottom: 16px;
    font-size: 0.9em;
}

/* ─── Danger zone (delete log entries) ─────────────────────────────────── */

.danger-zone {
    background: #fef9f9;
    border: 1px solid #fcd5d5;
    border-radius: 8px;
    padding: 16px;
    margin: 8px 0 24px;
}

.controls-panel {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: 12px 16px;
    margin: 8px 0 24px;
}
.control-row {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid #f1f5f9;
}
.control-row:last-child { border-bottom: none; }
.control-label { flex: 1 1 200px; min-width: 200px; font-weight: 500; }
.control-unit { color: #9ca3af; font-size: 0.85em; font-weight: 400; }
.control-input { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.control-input input { padding: 6px 8px; border: 1px solid #d1d5db; border-radius: 6px; max-width: 140px; }
.control-message { flex: 1 1 100%; font-size: 0.85em; color: #6b7280; }

@media (max-width: 720px) {
    .control-label { flex: 1 1 100%; }
    .control-input { flex: 1 1 100%; }
}

/* ─── Dashboard cards ──────────────────────────────────────────────────── */

.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 16px;
    margin: 16px 0 32px;
}

.device-card {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    padding: 16px 18px;
    text-decoration: none;
    color: inherit;
    display: flex;
    flex-direction: column;
    gap: 6px;
    transition: box-shadow .15s ease, transform .15s ease, border-color .15s ease;
}
.device-card:hover {
    box-shadow: 0 4px 12px rgba(0,0,0,.08);
    border-color: #d1d5db;
    transform: translateY(-1px);
}

.device-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}
.device-card-name {
    font-weight: 600;
    font-size: 1.05em;
    color: #111827;
}
.device-card-id {
    color: #6b7280;
    font-size: 0.85em;
    margin-top: -4px;
}
.device-card-meta {
    color: #6b7280;
    font-size: 0.85em;
    line-height: 1.5;
}

.device-card-channels {
    width: 100%;
    border-collapse: collapse;
    margin-top: 8px;
    background: transparent;
    border: none;
}
.device-card-channels td {
    padding: 4px 0;
    font-size: 0.92em;
    color: #374151;
    border: none;
}
.device-card-channels td:first-child {
    color: #6b7280;
    padding-right: 8px;
}
.device-card-value {
    text-align: right;
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    color: #111827;
}

/* Status pill */
.status-badge {
    font-size: 0.7em;
    padding: 3px 8px;
    border-radius: 999px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    white-space: nowrap;
}
.status-online   { background: #d1fae5; color: #065f46; }
.status-idle     { background: #fef3c7; color: #78350f; }
.status-stale    { background: #fed7aa; color: #7c2d12; }
.status-offline  { background: #fee2e2; color: #7f1d1d; }
.status-unknown  { background: #f3f4f6; color: #4b5563; }

/* ─── Bill estimate card (Home) ────────────────────────────────────────── */

.bill-card {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    padding: 16px 18px;
    margin: 8px 0 24px;
}
.bill-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 12px;
    margin-bottom: 12px;
}
.bill-stat {
    border-left: 3px solid #2563eb;
    padding: 4px 12px;
}
.bill-stat-label { color: #6b7280; font-size: 0.8em; }
.bill-stat-value { font-size: 1.3em; font-weight: 600; margin-top: 2px; }
.bill-stat-sub   { color: #6b7280; font-size: 0.8em; margin-top: 2px; }

.bill-meta {
    color: #6b7280;
    font-size: 0.85em;
    border-top: 1px solid #f1f5f9;
    padding-top: 10px;
    margin-bottom: 10px;
}

/* Section heading inside the bill card — used by "Last month" so it sits
   visually below the current-month grid without competing with the page <h2>. */
.bill-section {
    margin: 6px 0 8px;
    font-size: 0.95em;
    color: #374151;
    font-weight: 600;
}

/* Billing-compare page: period + date inputs sit on one row, wrap on narrow
   screens. Each group stacks label-above-control. */
.bc-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    align-items: flex-end;
    margin: 12px 0 20px;
}
.bc-control-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.bc-control-group label {
    font-size: 0.8em;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.bc-stepper {
    display: flex;
    align-items: center;
    gap: 6px;
}
.bc-chart-wrap {
    background: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    padding: 12px;
    margin: 16px 0 24px;
    height: 320px;
}

.bill-rate-edit {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
    background: #f9fafb;
    border-radius: 6px;
    padding: 10px 12px;
}
.bill-rate-edit label {
    flex: 1 1 100%;
    font-size: 0.85em;
    color: #374151;
}
.bill-rate-edit input {
    width: 120px;
    padding: 6px 8px;
    border: 1px solid #d1d5db;
    border-radius: 6px;
}

/* ─── Combined chart picker (Home) ─────────────────────────────────────── */

.combined-chart-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin: 8px 0 12px;
}
.combined-chart-toolbar .range-buttons { margin: 0; }

/* Compact picker — looks like another button in the toolbar until you open it. */
.channel-picker-compact {
    position: relative;
}
.channel-picker-compact > summary {
    list-style: none;
    cursor: pointer;
    user-select: none;
    /* inherits .btn styling because we apply class="btn" in the markup */
}
.channel-picker-compact > summary::-webkit-details-marker { display: none; }
.channel-picker-compact > summary::marker { content: ""; }

/* When open, the panel pops out below the button. Stays in flow on mobile
   (pushes the chart down) so we don't risk clipping; on desktop it's also in
   flow but anchored to the toolbar row visually. */
.picker-panel {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: 14px 16px;
    margin-top: 8px;
    max-width: 100%;
}

.picker-device {
    margin-top: 12px;
    padding-top: 10px;
    border-top: 1px solid #f1f5f9;
}
.picker-device:first-of-type { border-top: none; padding-top: 0; margin-top: 0; }
.picker-device-name {
    font-weight: 600;
    color: #374151;
    margin-bottom: 6px;
    font-size: 0.95em;
}
.picker-channels {
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.picker-channel-row {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 4px 0;
}
.picker-channel-row > .toggle:first-child { flex: 0 0 auto; min-width: 160px; }

/* Smaller, secondary-looking toggle for the "own axis" checkbox */
.toggle-mini {
    font-size: 0.85em;
    color: #6b7280;
}
.toggle-mini:hover { color: #2563eb; }

/* DeviceDetail page: each channel + its optional own-axis checkbox sit on
   a shared inline wrapper so both the channel-toggle row stays compact. */
.toggle-with-axis {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-right: 12px;
}
