/* ===== BillHub Automation — dark control-panel theme ===== */
:root {
  --bg: #0c1017;
  --bg-soft: #10161f;
  --surface: #151b26;
  --surface-2: #1b2332;
  --border: #232c3d;
  --border-soft: #1c2434;
  --text: #e8edf5;
  --muted: #8a94a8;
  --faint: #5c6579;
  --accent: #f5b841;
  --accent-soft: rgba(245, 184, 65, 0.14);
  --accent-glow: rgba(245, 184, 65, 0.35);
  --ok: #34d399;
  --ok-soft: rgba(52, 211, 153, 0.12);
  --danger: #f87171;
  --danger-soft: rgba(248, 113, 113, 0.12);
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 8px 28px rgba(2, 5, 10, 0.55);
  --font-ui: "Manrope", "Segoe UI", system-ui, sans-serif;
  --font-display: "Sora", "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html { color-scheme: dark; }
body {
  margin: 0;
  background:
    radial-gradient(1100px 500px at 85% -10%, rgba(245, 184, 65, 0.05), transparent 60%),
    radial-gradient(900px 600px at -10% 110%, rgba(80, 120, 255, 0.05), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.5;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { font-family: var(--font-display); text-wrap: balance; margin: 0; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }

#app { min-height: 100dvh; display: flex; flex-direction: column; }

/* ===== Header ===== */
.topbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: rgba(12, 16, 23, 0.82);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border-soft);
}
.brand { display: flex; align-items: center; gap: 10px; font-family: var(--font-display); font-weight: 700; font-size: 16px; letter-spacing: 0.2px; }
.brand .bolt {
  width: 30px; height: 30px; border-radius: 9px; flex: none;
  display: grid; place-items: center;
  background: linear-gradient(140deg, #f8ca6b, #eb9c1f);
  color: #241a05; box-shadow: 0 2px 12px var(--accent-glow);
}
.brand .bolt svg { width: 16px; height: 16px; }
.topbar .spacer { flex: 1; }
.conn-pill {
  display: inline-flex; align-items: center; gap: 7px;
  font-size: 12px; font-weight: 600; color: var(--muted);
  padding: 5px 11px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface);
}
.conn-pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); }
.conn-pill.live .dot { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
.conn-pill.live { color: var(--ok); border-color: rgba(52, 211, 153, 0.3); }

/* ===== Layout ===== */
main {
  flex: 1; width: 100%;
  max-width: 1060px; margin: 0 auto;
  padding: 20px 16px calc(88px + env(safe-area-inset-bottom));
}
@media (min-width: 900px) {
  main { padding-bottom: 40px; }
}
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { font-size: 22px; font-weight: 700; }
.page-head .sub { color: var(--muted); font-size: 13px; width: 100%; }
.page-head .grow { flex: 1; }

/* ===== Bottom nav (mobile) / top tabs (desktop) ===== */
.nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 40;
  display: flex; justify-content: space-around;
  background: rgba(16, 22, 31, 0.92);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--border-soft);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}
.nav button {
  flex: 1; max-width: 120px;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  background: none; border: 0; cursor: pointer;
  color: var(--faint); font-size: 11px; font-weight: 600;
  padding: 7px 4px; border-radius: 10px;
  transition: color 0.15s;
}
.nav button svg { width: 22px; height: 22px; }
.nav button.active { color: var(--accent); }
@media (min-width: 900px) {
  .nav {
    position: static; justify-content: center; gap: 8px;
    background: none; border: 0; backdrop-filter: none; padding: 0;
    max-width: 1060px; margin: 14px auto 0; padding: 0 16px;
    justify-content: flex-start;
  }
  .nav button {
    flex: none; flex-direction: row; gap: 8px; font-size: 13px;
    padding: 8px 16px; border-radius: 999px; border: 1px solid transparent;
  }
  .nav button svg { width: 17px; height: 17px; }
  .nav button.active { background: var(--accent-soft); border-color: rgba(245, 184, 65, 0.25); }
}

/* ===== Cards / grid ===== */
.grid { display: grid; gap: 14px; grid-template-columns: 1fr; }
@media (min-width: 640px) { .grid { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 980px) { .grid { grid-template-columns: repeat(3, 1fr); } }

.card {
  background: linear-gradient(180deg, var(--surface-2), var(--surface));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.device-card { padding: 16px; cursor: pointer; transition: border-color 0.15s, transform 0.15s; }
.device-card:hover { border-color: #33405a; transform: translateY(-1px); }
.device-card .head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.device-card .name { font-weight: 700; font-size: 16px; font-family: var(--font-display); flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device-card .mini-relays { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font-size: 12px; font-weight: 600; padding: 4px 10px;
  border-radius: 999px; border: 1px solid var(--border);
  background: var(--bg-soft); color: var(--muted);
}
.chip.on { background: var(--accent-soft); border-color: rgba(245, 184, 65, 0.35); color: var(--accent); }

.status-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--faint); flex: none; }
.status-dot.on { background: var(--ok); box-shadow: 0 0 0 4px var(--ok-soft); animation: pulse 2.2s infinite; }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 3px var(--ok-soft); } 50% { box-shadow: 0 0 0 6px rgba(52,211,153,0.05); } }
.status-txt { font-size: 12px; font-weight: 700; }
.status-txt.on { color: var(--ok); } .status-txt.off { color: var(--faint); }

/* ===== Relay rows ===== */
.relay-row {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-soft);
}
.relay-row:last-child { border-bottom: 0; }
.relay-row .info { flex: 1; min-width: 0; }
.relay-row .rname { font-weight: 700; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.relay-row .rmeta { font-size: 12px; color: var(--muted); }
.icon-btn {
  background: none; border: 0; cursor: pointer; color: var(--faint);
  width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center;
  flex: none; transition: background 0.15s, color 0.15s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }
.icon-btn svg { width: 17px; height: 17px; }

/* ===== Toggle switch ===== */
.switch { position: relative; width: 52px; height: 30px; flex: none; cursor: pointer; }
.switch input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.switch .track {
  position: absolute; inset: 0; border-radius: 999px;
  background: var(--bg-soft); border: 1px solid var(--border);
  transition: background 0.2s, border-color 0.2s, box-shadow 0.2s;
}
.switch .thumb {
  position: absolute; top: 3px; left: 3px; width: 24px; height: 24px;
  border-radius: 50%; background: #6b7488;
  transition: transform 0.22s cubic-bezier(0.34, 1.4, 0.64, 1), background 0.2s;
}
.switch input:checked ~ .track {
  background: linear-gradient(140deg, #f8ca6b, #eb9c1f);
  border-color: transparent;
  box-shadow: 0 0 14px var(--accent-glow);
}
.switch input:checked ~ .thumb { transform: translateX(22px); background: #fff; }
.switch input:disabled ~ .track { opacity: 0.45; }
.switch input:focus-visible ~ .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ===== Buttons ===== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 18px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text); font-weight: 700; font-size: 14px; cursor: pointer;
  transition: filter 0.15s, transform 0.05s;
}
.btn:active { transform: scale(0.98); }
.btn.primary {
  background: linear-gradient(140deg, #f8ca6b, #eb9c1f);
  color: #241a05; border-color: transparent;
  box-shadow: 0 4px 16px rgba(235, 156, 31, 0.25);
}
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { background: var(--danger-soft); border-color: rgba(248, 113, 113, 0.3); color: var(--danger); }
.btn.ghost { background: none; }
.btn.small { padding: 7px 13px; font-size: 13px; border-radius: 9px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }

/* ===== Forms ===== */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 12px; font-weight: 700; letter-spacing: 0.4px; text-transform: uppercase; color: var(--muted); }
.input, select.input {
  width: 100%; padding: 12px 14px;
  background: var(--bg-soft); border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.input::placeholder { color: var(--faint); }

/* ===== Auth screen ===== */
.auth-wrap {
  min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px;
}
.auth-card { width: 100%; max-width: 400px; padding: 30px 26px; }
.auth-card .brand { justify-content: center; margin-bottom: 6px; font-size: 19px; }
.auth-card .tag { text-align: center; color: var(--muted); font-size: 13px; margin: 0 0 24px; }
.auth-card h2 { font-size: 17px; margin-bottom: 16px; }
.auth-links { display: flex; justify-content: center; margin-top: 16px; font-size: 13px; }
.auth-links a { cursor: pointer; font-weight: 600; }
.auth-card.admin { border-color: rgba(245, 184, 65, 0.32); box-shadow: 0 8px 34px rgba(235, 156, 31, 0.12), var(--shadow); }
.portal-label {
  display: block; width: fit-content; margin: 0 auto 14px;
  font-size: 11px; font-weight: 800; letter-spacing: 1.4px; text-transform: uppercase;
  color: var(--accent); background: var(--accent-soft);
  border: 1px solid rgba(245, 184, 65, 0.3); border-radius: 999px; padding: 5px 14px;
}

/* ===== Modal / bottom sheet ===== */
.modal-back {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(4, 7, 12, 0.65);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
  display: grid; place-items: end center;
  animation: fadein 0.18s;
}
.modal {
  width: 100%; max-width: 460px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  padding: 22px 20px calc(22px + env(safe-area-inset-bottom));
  max-height: 88dvh; overflow-y: auto;
  animation: sheetup 0.24s cubic-bezier(0.32, 1, 0.5, 1);
}
@media (min-width: 640px) {
  .modal-back { place-items: center; }
  .modal { border-radius: 18px; padding-bottom: 22px; animation: popin 0.18s; }
}
.modal h3 { font-size: 17px; margin-bottom: 16px; }
.modal .actions { display: flex; gap: 10px; margin-top: 18px; }
.modal .actions .btn { flex: 1; }
@keyframes fadein { from { opacity: 0; } }
@keyframes sheetup { from { transform: translateY(40px); opacity: 0.5; } }
@keyframes popin { from { transform: scale(0.96); opacity: 0; } }

/* ===== Day picker ===== */
.days { display: flex; gap: 6px; flex-wrap: wrap; }
.days .day {
  width: 38px; height: 38px; border-radius: 10px;
  display: grid; place-items: center;
  background: var(--bg-soft); border: 1px solid var(--border);
  font-size: 12px; font-weight: 700; color: var(--muted); cursor: pointer;
  transition: all 0.12s; user-select: none;
}
.days .day.sel { background: var(--accent-soft); border-color: rgba(245, 184, 65, 0.4); color: var(--accent); }

/* ===== Schedule list ===== */
.sched-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: var(--radius-sm);
  background: var(--bg-soft); border: 1px solid var(--border-soft);
  margin-top: 8px;
}
.sched-item .time { font-family: var(--font-display); font-weight: 700; font-size: 16px; font-variant-numeric: tabular-nums; }
.sched-item .act { font-size: 11px; font-weight: 800; padding: 3px 9px; border-radius: 999px; }
.sched-item .act.on { background: var(--accent-soft); color: var(--accent); }
.sched-item .act.off { background: var(--surface-2); color: var(--muted); }
.sched-item .dstr { font-size: 11px; color: var(--faint); flex: 1; letter-spacing: 1px; }
.sched-item.disabled { opacity: 0.5; }

/* ===== Section / detail ===== */
.section { margin-bottom: 20px; }
.section > h2 { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.8px; color: var(--muted); margin-bottom: 10px; }
.kv {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; border-bottom: 1px solid var(--border-soft);
  font-size: 14px;
}
.kv:last-child { border-bottom: 0; }
.kv .k { color: var(--muted); width: 110px; flex: none; font-size: 13px; }
.kv .v { flex: 1; min-width: 0; font-family: ui-monospace, "Cascadia Mono", monospace; font-size: 13px; overflow-wrap: anywhere; }

/* ===== Tables (admin) ===== */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; min-width: 560px; }
th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: 0.7px; color: var(--muted); padding: 10px 14px; border-bottom: 1px solid var(--border); }
td { padding: 12px 14px; border-bottom: 1px solid var(--border-soft); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
.badge { font-size: 11px; font-weight: 800; padding: 3px 10px; border-radius: 999px; }
.badge.admin { background: var(--accent-soft); color: var(--accent); }
.badge.active { background: var(--ok-soft); color: var(--ok); }
.badge.disabled { background: var(--danger-soft); color: var(--danger); }

/* ===== Toast ===== */
#toast {
  position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  z-index: 90; opacity: 0; pointer-events: none;
  background: var(--surface-2); border: 1px solid var(--border);
  color: var(--text); font-weight: 600; font-size: 14px;
  padding: 12px 20px; border-radius: 12px; box-shadow: var(--shadow);
  transition: opacity 0.2s, transform 0.2s;
  max-width: calc(100vw - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.err { border-color: rgba(248, 113, 113, 0.4); color: var(--danger); }
@media (min-width: 900px) { #toast { bottom: 30px; } }

/* ===== Misc ===== */
.empty {
  text-align: center; color: var(--muted); padding: 48px 20px;
  border: 1px dashed var(--border); border-radius: var(--radius);
}
.empty .big { font-size: 34px; margin-bottom: 8px; }
.back-btn { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-weight: 600; font-size: 13px; cursor: pointer; background: none; border: 0; padding: 0; margin-bottom: 12px; }
.back-btn:hover { color: var(--text); }
.muted { color: var(--muted); font-size: 13px; }
.mono { font-family: ui-monospace, "Cascadia Mono", monospace; }
.cred-note {
  background: var(--accent-soft); border: 1px solid rgba(245, 184, 65, 0.3);
  color: var(--accent); border-radius: var(--radius-sm);
  padding: 12px 14px; font-size: 13px; margin-bottom: 14px;
}
.skeleton { color: var(--muted); text-align: center; padding: 40px; }

/* ===== Language switcher ===== */
.lang-sel {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 999px;
  padding: 5px 10px; font-size: 12px; font-weight: 600; cursor: pointer;
  max-width: 120px;
}
.lang-sel:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.kv .lang-sel { max-width: 140px; }

/* ===== Flash status ===== */
.flash-status {
  margin-top: 16px; padding: 16px; border-radius: var(--radius-sm);
  background: var(--bg-soft); border: 1px solid var(--border);
  font-weight: 600; font-size: 14px; line-height: 1.6;
}
.flash-status.ok { background: var(--ok-soft); border-color: rgba(52,211,153,0.35); }
.flash-status.err { background: var(--danger-soft); border-color: rgba(248,113,113,0.35); color: var(--danger); }
.flash-status .big { text-align: center; }
.spin {
  display: inline-block; width: 15px; height: 15px; vertical-align: -2px;
  border: 2px solid var(--faint); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.7s linear infinite; margin-right: 6px;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
  .spin { animation: spin 0.7s linear infinite !important; }
}
