/* ==================================================================
   Shrimp Farm Monitor — light theme, mobile-first
   The whole UI stays bright: no automatic dark mode.
   ================================================================== */

:root {
    color-scheme: light only;

    /* Surfaces */
    --plane:        #eef6f8;   /* page background */
    --plane-2:      #e3f0f4;   /* gradient partner */
    --surface:      #ffffff;   /* cards, panels */
    --surface-2:    #f4fafb;   /* inset tiles, table head */
    --surface-3:    #eaf4f7;   /* hover / chips */

    /* Ink */
    --ink:          #0f2c34;
    --ink-2:        #4d6670;
    --muted:        #8aa0a8;
    --line:         #dceaee;
    --line-2:       #cfe2e8;

    /* Brand */
    --brand:        #0d8ea8;
    --brand-2:      #0a7186;
    --brand-soft:   #e2f4f8;
    --brand-ink:    #ffffff;

    /* Status */
    --good:         #12a05a;
    --good-soft:    #e3f6ec;
    --warning:      #d98b06;
    --warning-soft: #fdf2dd;
    --critical:     #d84040;
    --critical-soft:#fdeaea;

    --radius:       16px;
    --radius-sm:    10px;
    --shadow:       0 1px 2px rgba(15,44,52,.05), 0 8px 24px rgba(15,44,52,.06);
    --shadow-sm:    0 1px 2px rgba(15,44,52,.06);
    --font:         system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    min-height: 100vh;
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.5;
    color: var(--ink);
    background-color: var(--plane);
    background-image: linear-gradient(180deg, var(--plane-2) 0, var(--plane) 100%);
    background-repeat: no-repeat;
    background-size: 100% 260px;
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; line-height: 1.25; }
a { color: inherit; }

.sr-only {
    position: absolute; width: 1px; height: 1px;
    padding: 0; margin: -1px; overflow: hidden;
    clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- Top bar ---------------------------------------------- */
.topbar {
    position: sticky; top: 0; z-index: 20;
    background: #ffffff;
    border-bottom: 1px solid var(--line);
    box-shadow: 0 1px 3px rgba(15,44,52,.05);
    padding-top: env(safe-area-inset-top);
}
.topbar__inner {
    display: flex; align-items: center; gap: 12px;
    max-width: 1100px; margin: 0 auto;
    padding: 12px 16px;
}
.topbar__back {
    display: inline-flex; align-items: center; justify-content: center;
    width: 40px; height: 40px; flex: 0 0 40px;
    border-radius: 50%;
    background: var(--brand-soft); color: var(--brand-2);
    text-decoration: none; font-size: 19px;
}
.topbar__back:hover { background: #d3edf3; }

/* Brand logo — assets/shpiot.png. Scales to fit whatever shape the file is:
   a square mark stays 40px, a wide wordmark is capped at 120px. */
.topbar__logo {
    display: inline-flex; align-items: center;
    flex: 0 0 auto; text-decoration: none;
}
.topbar__logo img {
    display: block;
    height: 40px; width: auto;
    max-width: 120px; object-fit: contain;
}
.topbar__titles { flex: 1 1 auto; min-width: 0; }
.topbar__title {
    font-size: 1.05rem; font-weight: 650; color: var(--ink);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar__sub {
    margin: 2px 0 0; font-size: .78rem; color: var(--muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.menu { position: relative; flex: 0 0 auto; }
.menu__button {
    list-style: none; cursor: pointer;
    display: inline-flex; align-items: center; justify-content: center;
    width: 40px; height: 40px; border-radius: 50%;
    background: var(--surface-3); color: var(--ink-2); font-size: 17px;
}
.menu__button::-webkit-details-marker { display: none; }
.menu__panel {
    position: absolute; right: 0; top: 48px; z-index: 30;
    min-width: 220px; padding: 8px;
    background: var(--surface); color: var(--ink);
    border: 1px solid var(--line); border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.menu__user {
    margin: 0 0 8px; padding: 8px 10px; font-size: .85rem;
    color: var(--ink-2); border-bottom: 1px solid var(--line);
    word-break: break-all;
}
.menu__item {
    display: block; padding: 10px; border-radius: var(--radius-sm);
    text-decoration: none; font-size: .95rem;
}
.menu__item:hover { background: var(--surface-3); }
.menu__item--danger { color: var(--critical); }

/* ---------- Layout ------------------------------------------------ */
.page {
    max-width: 1100px;
    margin: 0 auto;
    padding: 16px 16px 40px;
}
.footer {
    padding: 0 16px calc(24px + env(safe-area-inset-bottom));
    text-align: center; color: var(--muted); font-size: .78rem;
}

.panel {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 18px;
    margin-bottom: 16px;
}
.panel__head {
    display: flex; align-items: flex-start; justify-content: space-between;
    gap: 12px; margin-bottom: 4px;
}
.panel__title { font-size: 1.15rem; font-weight: 650; }
.panel__sub { margin: 2px 0 0; font-size: .82rem; color: var(--muted); }

.section-title {
    margin: 20px 0 10px;
    font-size: .72rem; font-weight: 700; letter-spacing: .09em;
    text-transform: uppercase; color: var(--brand-2);
}
.panel .section-title:first-child { margin-top: 0; }

.empty {
    padding: 36px 16px; text-align: center; color: var(--muted);
    background: var(--surface); border: 1px dashed var(--line-2);
    border-radius: var(--radius);
}
.hint { margin: 12px 0 0; font-size: .82rem; color: var(--muted); }

/* ---------- Summary strip ---------------------------------------- */
.summary {
    display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;
    margin-bottom: 16px;
}
.summary__item {
    background: var(--surface); border: 1px solid var(--line);
    border-radius: var(--radius-sm); padding: 12px 6px; text-align: center;
    box-shadow: var(--shadow-sm);
}
.summary__num { display: block; font-size: 1.4rem; font-weight: 700; }
.summary__lbl { display: block; font-size: .68rem; color: var(--muted); }
.summary__item--normal   { background: var(--good-soft);     border-color: #c9ecd9; }
.summary__item--watch    { background: var(--warning-soft);  border-color: #f5e2b8; }
.summary__item--critical { background: var(--critical-soft); border-color: #f6cdcd; }
.summary__item--normal   .summary__num { color: #0d7a44; }
.summary__item--watch    .summary__num { color: #a76a04; }
.summary__item--critical .summary__num { color: #b93333; }
.summary__item--normal   .summary__lbl,
.summary__item--watch    .summary__lbl,
.summary__item--critical .summary__lbl { color: var(--ink-2); }

/* ---------- Node cards -------------------------------------------- */
.cards { display: grid; gap: 12px; }
.card {
    background: var(--surface); border: 1px solid var(--line);
    border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
}
.card__main { display: block; padding: 16px; text-decoration: none; color: inherit; }
.card__head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.card__title { font-size: 1.02rem; font-weight: 650; }
.card__metrics {
    display: grid; grid-template-columns: repeat(3, 1fr);
    gap: 8px; margin-top: 14px;
}
.metric {
    background: var(--surface-2); border: 1px solid var(--line);
    border-radius: var(--radius-sm); padding: 9px;
}
.metric__lbl { display: block; font-size: .68rem; color: var(--muted); }
.metric__val { display: block; font-size: 1.1rem; font-weight: 650; }
.metric__val small { font-size: .62rem; font-weight: 500; color: var(--muted); margin-left: 2px; }
.card__foot { margin: 12px 0 0; font-size: .74rem; color: var(--muted); }
.card__actions {
    display: flex; gap: 8px; padding: 12px 16px 14px;
    border-top: 1px solid var(--line); background: var(--surface-2);
}
.card__actions .btn { flex: 1; }

/* ---------- Badges & pills ---------------------------------------- */
.badge {
    display: inline-flex; align-items: center; gap: 5px;
    padding: 3px 10px; border-radius: 999px;
    font-size: .72rem; font-weight: 650; white-space: nowrap;
    border: 1px solid transparent;
}
.badge::before {
    content: ""; width: 7px; height: 7px; border-radius: 50%;
    background: currentColor;
}
.badge--lg { font-size: .82rem; padding: 5px 12px; }
.badge--normal   { color: #0d7a44; background: var(--good-soft);     border-color: #c2e9d3; }
.badge--watch    { color: #a76a04; background: var(--warning-soft);  border-color: #f2dcae; }
.badge--critical { color: #b93333; background: var(--critical-soft); border-color: #f5c6c6; }
.badge--none     { color: var(--ink-2); background: var(--surface-3); border-color: var(--line-2); }

.pill {
    display: inline-block; padding: 2px 9px; border-radius: 999px;
    font-size: .7rem; font-weight: 650;
    border: 1px solid var(--line-2); background: var(--surface-2); color: var(--muted);
}
.pill--on { color: #0d7a44; background: var(--good-soft); border-color: #c2e9d3; }

/* ---------- Readings ---------------------------------------------- */
.readings { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.reading {
    background: var(--surface-2); border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    padding: 12px; min-width: 0;
}
.reading__lbl { display: block; font-size: .68rem; color: var(--muted); }
.reading__val { display: block; font-size: 1.2rem; font-weight: 650; }
.reading__val small { font-size: .66rem; font-weight: 500; color: var(--muted); }
.reading__val--sm { font-size: .82rem; font-weight: 600; }

@media (max-width: 420px) {
    .readings { grid-template-columns: repeat(2, 1fr); }
    .summary { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- Controls ---------------------------------------------- */
.control {
    display: flex; align-items: center; justify-content: space-between;
    gap: 14px; padding: 16px 0; border-bottom: 1px solid var(--line);
}
.control:last-child { border-bottom: 0; padding-bottom: 0; }
.control--stack { flex-direction: column; align-items: stretch; }
.control__text { display: flex; flex-direction: column; }
.control__name { font-weight: 600; }
.control__state { font-size: .8rem; color: var(--muted); }

.switch { position: relative; display: inline-block; flex: 0 0 auto; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch__track {
    display: block; width: 56px; height: 32px; border-radius: 999px;
    background: #dbe7ea; transition: background .18s ease;
}
.switch__thumb {
    display: block; width: 26px; height: 26px; margin: 3px; border-radius: 50%;
    background: #fff; box-shadow: 0 1px 3px rgba(15,44,52,.28);
    transition: transform .18s ease;
}
.switch input:checked + .switch__track { background: var(--good); }
.switch input:checked + .switch__track .switch__thumb { transform: translateX(24px); }
.switch input:focus-visible + .switch__track { outline: 3px solid var(--brand); outline-offset: 2px; }
.switch input:disabled + .switch__track { opacity: .55; }

.segmented {
    display: grid; grid-template-columns: repeat(3, 1fr);
    gap: 4px; margin-top: 12px;
    background: var(--surface-2); border: 1px solid var(--line);
    border-radius: var(--radius-sm); padding: 4px;
}
.segmented__btn {
    appearance: none; border: 0; cursor: pointer;
    padding: 10px 6px; border-radius: 7px;
    background: transparent; color: var(--ink-2);
    font: inherit; font-size: .86rem; font-weight: 600;
    transition: background .15s ease, color .15s ease;
}
.segmented__btn:hover { background: var(--surface-3); }
.segmented__btn.is-active {
    background: var(--brand); color: #fff;
    box-shadow: 0 1px 3px rgba(13,142,168,.35);
}
.segmented__btn:disabled { opacity: .6; cursor: default; }

/* ---------- Buttons ----------------------------------------------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center;
    gap: 6px; min-height: 44px; padding: 0 16px;
    border: 1px solid var(--line-2); border-radius: var(--radius-sm);
    background: var(--surface); color: var(--ink);
    font: inherit; font-size: .92rem; font-weight: 600;
    text-decoration: none; cursor: pointer;
    transition: background .15s ease, border-color .15s ease, opacity .15s ease;
}
.btn:hover { background: var(--surface-3); border-color: var(--brand); }
.btn--primary {
    background: var(--brand); border-color: var(--brand); color: #fff;
    box-shadow: 0 1px 3px rgba(13,142,168,.3);
}
.btn--primary:hover { background: var(--brand-2); border-color: var(--brand-2); }
.btn--ghost { background: var(--surface); color: var(--brand-2); }
.btn--block { width: 100%; }
.btn--sm { min-height: 36px; padding: 0 12px; font-size: .82rem; }
.btn.is-disabled { pointer-events: none; opacity: .45; }

.btn-row { display: grid; gap: 10px; margin-bottom: 16px; }

/* ---------- Forms ------------------------------------------------- */
.form { display: grid; gap: 14px; }
.field { display: block; }
.field__label {
    display: block; margin-bottom: 6px;
    font-size: .78rem; font-weight: 600; color: var(--ink-2);
}
.field__input {
    width: 100%; min-height: 46px; padding: 10px 12px;
    border: 1px solid var(--line-2); border-radius: var(--radius-sm);
    background: var(--surface); color: var(--ink);
    font: inherit; font-size: 16px; /* 16px stops iOS zoom-on-focus */
}
.field__input::placeholder { color: #a9bcc2; }
.field__input:focus {
    outline: none; border-color: var(--brand);
    box-shadow: 0 0 0 3px rgba(13,142,168,.18);
}
.field__input--area { min-height: 92px; resize: vertical; line-height: 1.45; }
.field__input.has-error { border-color: var(--critical); background: #fffafa; }
.field__input.has-error:focus { box-shadow: 0 0 0 3px rgba(216,64,64,.16); }
.field__note { font-weight: 500; color: var(--muted); }
.field__error {
    display: block; margin-top: 5px;
    font-size: .78rem; font-weight: 600; color: var(--critical);
}
.field__help { margin: 0 0 14px; font-size: .8rem; color: var(--muted); }
.field-grid { display: grid; gap: 14px; }

.panel > .field + .field,
.panel > .field + .field-grid,
.panel > .field-grid + .field { margin-top: 14px; }
.field-grid > .field + .field { margin-top: 0; }

.link { color: var(--brand-2); font-weight: 600; }

/* API token row */
.token {
    display: flex; align-items: center; justify-content: space-between;
    gap: 12px; flex-wrap: wrap;
    padding: 12px 14px;
    background: var(--surface-2); border: 1px solid var(--line);
    border-radius: var(--radius-sm);
}
.token__value {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 1.05rem; font-weight: 700; letter-spacing: .12em;
    color: var(--brand-2);
}

.filters {
    display: grid; gap: 12px; margin-top: 16px;
    padding-top: 16px; border-top: 1px solid var(--line);
}
.filters__actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.quick-range { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.chip {
    padding: 6px 12px; border-radius: 999px;
    border: 1px solid var(--line-2); background: var(--surface-2);
    font-size: .78rem; font-weight: 600; color: var(--ink-2);
    text-decoration: none;
}
.chip:hover { background: var(--brand-soft); border-color: var(--brand); color: var(--brand-2); }
.chip.is-active { background: var(--brand); border-color: var(--brand); color: #fff; }

.alert {
    margin: 0 0 14px; padding: 10px 12px;
    border-radius: var(--radius-sm); font-size: .86rem;
    border: 1px solid transparent;
}
.alert--error   { color: #b93333; background: var(--critical-soft); border-color: #f5c6c6; }
.alert--info    { color: var(--brand-2); background: var(--brand-soft); border-color: #c6e8f0; }
.alert--success { color: #0d7a44; background: var(--good-soft); border-color: #c2e9d3; }

/* ---------- Charts ------------------------------------------------ */
.chart-box { position: relative; height: 280px; }
@media (min-width: 720px) { .chart-box { height: 340px; } }

/* ---------- Tables ------------------------------------------------ */
.table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
}
.table-wrap--desktop { display: none; }

.table { width: 100%; border-collapse: collapse; font-size: .88rem; }
.table th, .table td {
    padding: 12px; text-align: left;
    border-bottom: 1px solid var(--line); white-space: nowrap;
}
.table thead th {
    position: sticky; top: 0;
    background: var(--surface-2); color: var(--brand-2);
    font-size: .7rem; font-weight: 700; letter-spacing: .05em;
    text-transform: uppercase;
    border-bottom: 1px solid var(--line-2);
}
.table thead th small {
    font-weight: 500; letter-spacing: 0; text-transform: none; color: var(--muted);
}
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: var(--surface-2); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table--compact th, .table--compact td { padding: 10px; }
.nowrap { white-space: nowrap; }
.row-link { cursor: pointer; }
.actions-col { text-align: right; }
.actions-col .btn + .btn { margin-left: 6px; }

.swipe-hint { margin: -4px 0 10px; font-size: .74rem; color: var(--muted); }
@media (min-width: 900px) { .swipe-hint { display: none; } }

.pager {
    display: flex; align-items: center; justify-content: space-between;
    gap: 10px; margin-top: 14px;
}
.pager__info { font-size: .8rem; color: var(--muted); }

/* ---------- Login ------------------------------------------------- */
.body--auth {
    min-height: 100vh;
    background: linear-gradient(165deg, #dff2f7 0%, #eef8fa 45%, #f6fbfc 100%);
}
.auth {
    display: flex; align-items: center; justify-content: center;
    min-height: 100vh; padding: 24px 16px;
}
.auth__card {
    width: 100%; max-width: 400px; padding: 30px 24px;
    background: var(--surface); border: 1px solid var(--line);
    border-radius: 20px;
    box-shadow: 0 2px 4px rgba(15,44,52,.04), 0 20px 44px rgba(15,44,52,.10);
}
.auth__brand { text-align: center; margin-bottom: 24px; }
.auth__logo {
    display: block; margin: 0 auto 14px;
    width: auto; height: auto;
    max-width: 190px; max-height: 90px; object-fit: contain;
}
.auth__brand h1 { font-size: 1.3rem; font-weight: 700; }
.auth__brand p { margin: 8px 0 0; font-size: .84rem; color: var(--muted); }

/* ---------- Breakpoints ------------------------------------------- */
@media (min-width: 768px) {
    .page { padding: 24px 24px 56px; }
    .cards { display: none; }              /* table takes over */
    .table-wrap--desktop { display: block; }
    .summary { grid-template-columns: repeat(4, 1fr); }
    .filters { grid-template-columns: 1fr 1fr auto; align-items: end; }
    .filters__actions { grid-template-columns: auto auto; }
    .btn-row { grid-template-columns: 1fr 1fr; }
    .readings { grid-template-columns: repeat(3, 1fr); }
    .control--stack { flex-direction: row; align-items: center; }
    .control--stack .segmented { margin-top: 0; width: 340px; }
    .field-grid { grid-template-columns: 1fr 1fr; }
}
