@import url("fonts/fonts.css");

/* ---------- Tokens ---------- */
:root {
  color-scheme: light;
  --page:           #f3f5f9;
  --surface-1:      #ffffff;
  --surface-2:      #eef1f6;
  --surface-3:      #e2e7ef;
  --text-primary:   #0e1525;
  --text-secondary: #4a5568;
  --text-muted:     #8e99ab;
  --border:         rgba(14,21,37,0.07);
  --border-strong:  rgba(14,21,37,0.14);
  --accent:         #e58a00;              /* electric amber */
  --accent-2:       #ff6a3d;
  --accent-text:    #b86e00;
  --accent-soft:    rgba(245,158,11,0.14);
  --accent-ink:     #1b1203;
  --accent-grad:    linear-gradient(135deg, #ffc53d 0%, #ff8a1f 55%, #ff5a3c 100%);
  --good:           #0f9f6e;
  --good-soft:      rgba(16,185,129,0.13);
  --warn:           #e5484d;
  --warn-soft:      rgba(229,72,77,0.12);
  --info:           #3b82f6;
  --glass:          rgba(255,255,255,0.72);
  --glass-border:   rgba(255,255,255,0.9);
  --shadow:         0 1px 2px rgba(14,21,37,.04), 0 8px 28px -6px rgba(14,21,37,.10);
  --shadow-lg:      0 24px 64px -12px rgba(14,21,37,.28), 0 2px 6px rgba(14,21,37,.06);
  --radius:         20px;
  --radius-sm:      12px;
  --nav-h:          64px;
  --header-h:       60px;
  --dock-space:     calc(var(--nav-h) + 20px + env(safe-area-inset-bottom));
  --font: 'Inter', system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-display: 'Inter', system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page:           #080b12;
    --surface-1:      #111622;
    --surface-2:      #182030;
    --surface-3:      #232c3f;
    --text-primary:   #edf1f7;
    --text-secondary: #a5afc0;
    --text-muted:     #677288;
    --border:         rgba(255,255,255,0.07);
    --border-strong:  rgba(255,255,255,0.13);
    --accent:         #fbb024;
    --accent-text:    #fbbf3c;
    --accent-soft:    rgba(251,176,36,0.14);
    --accent-ink:     #1b1203;
    --good:           #34d399;
    --good-soft:      rgba(52,211,153,0.14);
    --warn:           #ff6b6f;
    --warn-soft:      rgba(255,107,111,0.14);
    --info:           #60a5fa;
    --glass:          rgba(17,22,34,0.66);
    --glass-border:   rgba(255,255,255,0.08);
    --shadow:         0 1px 2px rgba(0,0,0,.4), 0 8px 28px -6px rgba(0,0,0,.5);
    --shadow-lg:      0 24px 64px -12px rgba(0,0,0,.7), 0 2px 6px rgba(0,0,0,.4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page:           #080b12;
  --surface-1:      #111622;
  --surface-2:      #182030;
  --surface-3:      #232c3f;
  --text-primary:   #edf1f7;
  --text-secondary: #a5afc0;
  --text-muted:     #677288;
  --border:         rgba(255,255,255,0.07);
  --border-strong:  rgba(255,255,255,0.13);
  --accent:         #fbb024;
  --accent-text:    #fbbf3c;
  --accent-soft:    rgba(251,176,36,0.14);
  --accent-ink:     #1b1203;
  --good:           #34d399;
  --good-soft:      rgba(52,211,153,0.14);
  --warn:           #ff6b6f;
  --warn-soft:      rgba(255,107,111,0.14);
  --info:           #60a5fa;
  --glass:          rgba(17,22,34,0.66);
  --glass-border:   rgba(255,255,255,0.08);
  --shadow:         0 1px 2px rgba(0,0,0,.4), 0 8px 28px -6px rgba(0,0,0,.5);
  --shadow-lg:      0 24px 64px -12px rgba(0,0,0,.7), 0 2px 6px rgba(0,0,0,.4);
}
@media (min-width: 820px) { :root { --dock-space: 0px; } }

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
/* `hidden` must win over component display rules (flex/grid). */
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  background: var(--page);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  font-feature-settings: "cv11", "ss01";
}
h1, h2, h3 { font-family: var(--font-display); font-weight: 700; letter-spacing: -0.025em; margin: 0; }
h1 { font-size: 1.75rem; }
h2 { font-size: 1.05rem; letter-spacing: -0.015em; }
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--text-muted); }
.secondary { color: var(--text-secondary); }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
svg.icon { width: 20px; height: 20px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.glass {
  background: var(--glass);
  backdrop-filter: saturate(1.8) blur(20px);
  -webkit-backdrop-filter: saturate(1.8) blur(20px);
  border: 1px solid var(--glass-border);
}

/* ---------- App shell ---------- */
.app-header {
  position: sticky; top: 0; z-index: 30;
  height: calc(var(--header-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
  display: flex; align-items: center; gap: 12px;
  background: color-mix(in srgb, var(--page) 78%, transparent);
  backdrop-filter: saturate(1.8) blur(20px);
  -webkit-backdrop-filter: saturate(1.8) blur(20px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; font-weight: 700; font-size: 1.08rem; letter-spacing: -0.02em; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 11px; display: grid; place-items: center;
  background: var(--accent-grad); color: #1b1203;
  box-shadow: 0 6px 16px -4px rgba(255,122,31,.55), inset 0 1px 0 rgba(255,255,255,.45);
}
.brand-mark svg { width: 18px; height: 18px; stroke-width: 2.2; fill: rgba(27,18,3,.15); }
.top-nav { display: none; gap: 2px; margin-left: 20px; padding: 4px; border-radius: 14px; background: var(--surface-2); }
.top-nav a {
  display: flex; align-items: center; gap: 8px; padding: 7px 14px; border-radius: 10px;
  text-decoration: none; color: var(--text-secondary); font-weight: 600; font-size: .9rem;
}
.top-nav a:hover { color: var(--text-primary); }
.top-nav a[aria-current="page"] { background: var(--surface-1); color: var(--text-primary); box-shadow: var(--shadow); }
.top-nav a[aria-current="page"] svg { color: var(--accent); }
.header-actions { margin-left: auto; display: flex; gap: 4px; align-items: center; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 14px; border: 0; background: transparent;
  display: grid; place-items: center; cursor: pointer; color: var(--text-secondary);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text-primary); }

/* Floating dock on phones */
.bottom-nav {
  position: fixed; z-index: 30;
  left: max(12px, env(safe-area-inset-left)); right: max(12px, env(safe-area-inset-right));
  bottom: calc(10px + env(safe-area-inset-bottom));
  height: var(--nav-h); padding: 6px;
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px;
  border-radius: 24px; box-shadow: var(--shadow-lg);
  background: var(--glass);
  backdrop-filter: saturate(1.8) blur(22px);
  -webkit-backdrop-filter: saturate(1.8) blur(22px);
  border: 1px solid var(--glass-border);
}
.bottom-nav a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  border-radius: 18px; text-decoration: none; color: var(--text-muted); font-size: .68rem; font-weight: 600;
  transition: background .15s, color .15s;
}
.bottom-nav a svg { width: 23px; height: 23px; }
.bottom-nav a[aria-current="page"] { color: var(--text-primary); background: var(--accent-soft); }
.bottom-nav a[aria-current="page"] svg { color: var(--accent); }

.page {
  max-width: 1100px; margin: 0 auto;
  padding: 24px max(16px, env(safe-area-inset-right)) calc(var(--dock-space) + 24px) max(16px, env(safe-area-inset-left));
}
.page-full { padding: 0; max-width: none; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head p { margin: 4px 0 0; color: var(--text-secondary); font-size: .9rem; }

@media (min-width: 820px) {
  .top-nav { display: flex; }
  .bottom-nav { display: none; }
  .page { padding-bottom: 48px; }
}

/* ---------- Alerts ---------- */
.bell { position: relative; }
.bell-badge {
  position: absolute; top: 6px; right: 5px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: #f59e0b; color: #1b1203; font-size: .68rem; font-weight: 800; display: grid; place-items: center;
  box-shadow: 0 0 0 2px var(--page);
}
.bell-badge.critical { background: var(--warn); color: #fff; animation: badge-pulse 1.6s ease-in-out infinite; }
@keyframes badge-pulse { 50% { box-shadow: 0 0 0 2px var(--page), 0 0 0 6px color-mix(in srgb, var(--warn) 25%, transparent); } }
.alert-banner {
  position: fixed; z-index: 29; left: 50%; transform: translateX(-50%);
  top: calc(var(--header-h) + env(safe-area-inset-top) + 10px); width: min(calc(100% - 24px), 640px);
  display: flex; align-items: center; gap: 12px; padding: 10px 10px 10px 14px; border-radius: 18px;
  background: linear-gradient(135deg, #e5484d, #c62a3a); color: #fff; box-shadow: 0 16px 40px -12px rgba(229,72,77,.6);
  cursor: pointer; animation: toast-in .25s ease-out;
}
.alert-banner svg { width: 22px; height: 22px; flex: none; }
.alert-banner .t { flex: 1; min-width: 0; font-size: .88rem; line-height: 1.3; }
.alert-banner .t b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.alert-banner .t span { opacity: .9; }
.alert-banner button { border: 0; background: rgba(255,255,255,.2); color: #fff; font-weight: 700; border-radius: 12px; padding: 8px 12px; cursor: pointer; white-space: nowrap; }
.has-banner .page:not(.page-full) { padding-top: 100px; }
.has-banner .plan-shell .float.tl, .has-banner .plan-shell .float.tr { top: 88px; }
.has-banner .plan-shell .hint { top: 146px; }
@media (min-width: 900px) { .has-banner .plan-shell .panel, .has-banner .plan-shell .popup { top: 146px; } }
.alert-list { display: grid; gap: 10px; }
.alert-item { display: flex; gap: 12px; align-items: flex-start; padding: 12px 14px; border-radius: 16px; background: var(--surface-2); }
.alert-item .ai-icon { width: 36px; height: 36px; border-radius: 11px; display: grid; place-items: center; flex: none; background: var(--accent-soft); color: var(--accent-text); }
.alert-item.critical .ai-icon { background: var(--warn-soft); color: var(--warn); }
.alert-item.critical { box-shadow: inset 3px 0 0 var(--warn); }
.alert-item.warning { box-shadow: inset 3px 0 0 #f59e0b; }
.alert-item.ended { opacity: .7; box-shadow: none; }
.alert-item .ai-text { flex: 1; min-width: 0; }
.alert-item .ai-text b { display: block; font-size: .92rem; }
.alert-item .ai-text small { display: block; color: var(--text-muted); font-size: .76rem; margin-top: 2px; }
.alert-item .ai-text span { font-size: .86rem; color: var(--text-secondary); }
.alert-item .btn { min-height: 34px; padding: 0 12px; font-size: .8rem; }
.alert-section { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--text-muted); margin: 18px 0 8px; }

/* ---------- Components ---------- */
.pill.warnish { background: rgba(245,158,11,.16); color: var(--accent-text); }

/* ---------- What is plugged in (editor shared by plan and devices) ---------- */
.loads { display: grid; gap: 6px; }
.load-row { display: grid; grid-template-columns: 70px auto 28px 24px 28px 1fr 28px; align-items: center; gap: 4px 6px; padding: 8px 8px 8px 10px; border-radius: 12px; background: var(--surface-2); font-size: .84rem; }
.load-row .ln { grid-column: 1 / -1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.load-row .qb.del { grid-column: -2; }
.load-row .lw { min-height: 32px !important; padding: 0 6px; text-align: right; font-size: .85rem; border-radius: 8px; }
.load-row .lu, .load-row .lq { color: var(--text-muted); font-size: .78rem; text-align: center; }
.load-row .lq { color: var(--text-primary); font-weight: 700; }
.qb { width: 28px; height: 28px; border-radius: 8px; border: 1px solid var(--border-strong); background: var(--surface-1); cursor: pointer; font-weight: 700; line-height: 1; padding: 0; }
.qb.del { border-color: transparent; background: transparent; color: var(--text-muted); font-size: 1.1rem; }
.qb.del:hover { color: var(--warn); }
.load-row .ln small { font-weight: 500; color: var(--text-muted); }
.load-typ { font-size: .76rem; color: var(--text-muted); margin: 2px 0 2px; }
.load-empty { font-size: .82rem; color: var(--text-secondary); margin: 6px 0 0; }
.load-note { font-size: .8rem; line-height: 1.35; padding: 8px 10px; border-radius: 10px; margin-top: 6px; background: var(--surface-2); color: var(--text-secondary); }
.load-note.bad { background: var(--warn-soft); color: var(--warn); }
.load-note.warn { background: rgba(245,158,11,.14); color: var(--accent-text); }
.load-note.shelly { background: color-mix(in srgb, var(--info) 12%, transparent); color: var(--text-primary); }

/* ---------- Load bar (current vs. limit) ---------- */
.load { display: grid; gap: 6px; }
.load-track { position: relative; height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.load-track i { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--text-muted); opacity: .5; }
.load-fill { height: 100%; border-radius: 4px; background: linear-gradient(90deg, #34d399, #10b981); transition: width .6s ease; }
.load-fill.warn { background: linear-gradient(90deg, #fbbf24, #f59e0b); }
.load-fill.crit { background: linear-gradient(90deg, #fb7185, #e5484d); }
.load-text { display: flex; justify-content: space-between; font-size: .76rem; color: var(--text-muted); font-weight: 600; }

.card {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.btn {
  min-height: 44px; padding: 0 18px; border-radius: 14px; border: 1px solid var(--border-strong);
  background: var(--surface-1); color: var(--text-primary); font-weight: 600; font-size: .92rem;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; cursor: pointer;
  text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent-grad); border-color: transparent; color: var(--accent-ink); box-shadow: 0 6px 18px -6px rgba(255,122,31,.6), inset 0 1px 0 rgba(255,255,255,.35); }
.btn-primary:hover { background: var(--accent-grad); filter: brightness(1.05); }
.btn-danger { color: var(--warn); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-block { width: 100%; }
.btn .spinner { width: 16px; height: 16px; }

.pill {
  display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px; border-radius: 999px;
  font-size: .74rem; font-weight: 600; background: var(--surface-2); color: var(--text-secondary); white-space: nowrap;
}
.pill svg { width: 13px; height: 13px; }
.pill.on { background: var(--good-soft); color: var(--good); }
.pill.off { background: var(--surface-2); color: var(--text-muted); }
.pill.bad { background: var(--warn-soft); color: var(--warn); }
.pill.accent { background: var(--accent-soft); color: var(--accent-text); }
.dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }

.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: .8rem; font-weight: 600; color: var(--text-secondary); }
.field small { color: var(--text-muted); font-size: .78rem; }
.input, select.input {
  width: 100%; min-height: 46px; padding: 0 14px; border-radius: 12px;
  border: 1px solid var(--border-strong); background: var(--page); font-size: 16px; /* no iOS zoom */
}
.input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }

.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; padding: 4px; background: var(--surface-2); border-radius: 12px; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.segmented span {
  display: flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px; border-radius: 9px;
  font-size: .85rem; font-weight: 600; color: var(--text-secondary); text-align: center; padding: 0 6px;
}
.segmented input:checked + span { background: var(--surface-1); color: var(--text-primary); box-shadow: var(--shadow); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }
.segmented input:disabled + span { opacity: .45; }

.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; }
.switch-row .text { display: grid; gap: 2px; }
.switch-row .text small { color: var(--text-muted); font-size: .78rem; }
.toggle { position: relative; width: 50px; height: 30px; flex: none; }
.toggle input { position: absolute; inset: 0; opacity: 0; cursor: pointer; margin: 0; }
.toggle span { position: absolute; inset: 0; border-radius: 999px; background: var(--surface-3); transition: background .15s; pointer-events: none; }
.toggle span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .15s; }
.toggle input:checked + span { background: var(--good); }
.toggle input:checked + span::after { transform: translateX(20px); }
.toggle input:disabled + span { opacity: .4; }
.toggle input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.alert { padding: 12px 14px; border-radius: 12px; font-size: .88rem; display: flex; gap: 10px; align-items: flex-start; }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.info { background: var(--accent-soft); color: var(--text-primary); }
.alert a { font-weight: 600; }

/* Bottom sheet on phones, centered dialog on desktop */
dialog.sheet {
  border: 0; padding: 0; margin: auto auto 0; width: 100%; max-width: 560px;
  max-height: min(92dvh, 900px); border-radius: 22px 22px 0 0;
  background: var(--surface-1); color: var(--text-primary); box-shadow: var(--shadow-lg);
}
dialog.sheet::backdrop { background: rgba(10,8,6,.45); backdrop-filter: blur(2px); }
dialog.sheet[open] { animation: sheet-in .22s cubic-bezier(.2,.8,.2,1); display: flex; flex-direction: column; }
@keyframes sheet-in { from { transform: translateY(40px); opacity: 0; } }
.sheet-head { display: flex; align-items: center; gap: 12px; padding: 18px 18px 8px; }
.sheet-head h2 { flex: 1; }
.sheet-body { padding: 8px 18px 18px; overflow-y: auto; }
.sheet-foot { display: flex; gap: 10px; padding: 12px 18px calc(16px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border); }
.sheet-foot .btn { flex: 1; }
.sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--surface-3); margin: 8px auto 0; }
@media (min-width: 640px) {
  dialog.sheet { margin: auto; border-radius: 22px; }
  .sheet-grip { display: none; }
  .sheet-foot { padding-bottom: 16px; }
}

.toast-host { position: fixed; z-index: 100; left: 50%; transform: translateX(-50%); bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 14px); display: grid; gap: 8px; width: min(92vw, 420px); pointer-events: none; }
@media (min-width: 820px) { .toast-host { bottom: 24px; } }
.toast {
  pointer-events: auto; padding: 12px 16px; border-radius: 12px; background: var(--text-primary); color: var(--page);
  font-size: .9rem; font-weight: 500; box-shadow: var(--shadow-lg); display: flex; gap: 10px; align-items: center;
  animation: toast-in .2s ease-out;
}
.toast.error { background: var(--warn); color: #fff; }
.toast.success { background: var(--good); color: #fff; }
@keyframes toast-in { from { transform: translateY(10px); opacity: 0; } }

.spinner { width: 20px; height: 20px; border-radius: 50%; border: 2.5px solid currentColor; border-right-color: transparent; animation: spin .7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

.empty { text-align: center; padding: 40px 20px; color: var(--text-secondary); }
.empty svg { width: 40px; height: 40px; color: var(--text-muted); margin-bottom: 10px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/*
 * Touch devices: no backdrop blur. Safari/WebKit re-renders blurred backdrops on every frame while
 * anything on the page moves (measured: it was the main cause of jank when panning the plan on iPhone).
 */
@media (pointer: coarse) {
  .app-header { backdrop-filter: none; -webkit-backdrop-filter: none; background: var(--page); }
  .bottom-nav, .glass { backdrop-filter: none; -webkit-backdrop-filter: none; background: var(--surface-1); }
  dialog.sheet::backdrop { backdrop-filter: none; -webkit-backdrop-filter: none; }
  /* Large blurred shadows over a moving layer are costly too: use a hairline border instead. */
  .bottom-nav { box-shadow: 0 0 0 1px var(--border-strong); }
  body:has(.plan-shell) .plan-shell .chip-group,
  body:has(.plan-shell) .plan-shell .hud,
  body:has(.plan-shell) .plan-shell .popup,
  body:has(.plan-shell) .plan-shell .panel,
  body:has(.plan-shell) .plan-shell .locks-pop { box-shadow: 0 0 0 1px var(--border-strong) !important; }
  body:has(.plan-shell) .plan-shell .chip-btn[aria-pressed="true"] { box-shadow: none; }
}
