:root {
  --bg: #0b1220;
  --bg-2: #121b2e;
  --card: #162034;
  --line: #243049;
  --text: #e8eef8;
  --muted: #93a3bd;
  --accent: #f5a623;
  --accent-2: #4da3ff;
  --ok: #3ecf8e;
  --danger: #ff6b6b;
  --radius: 14px;
  --tap: 44px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  padding-bottom: env(safe-area-inset-bottom);
}

body { overscroll-behavior-y: contain; }

h1, h2, h3 { margin: 0; font-weight: 650; }
h1 { font-size: 1.15rem; letter-spacing: .2px; }
h2 { font-size: 1rem; margin-bottom: .6rem; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: .75rem;
  padding: calc(env(safe-area-inset-top) + .7rem) .9rem .7rem;
  background: linear-gradient(180deg, #0d1526 0%, #0b1220 100%);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: .65rem; min-width: 0; }
.sub { margin: 0; font-size: .78rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.beacon {
  width: 16px; height: 16px; border-radius: 50%;
  background: #3a4761; box-shadow: 0 0 0 0 rgba(245,166,35,.55);
  flex: 0 0 auto; transition: background .25s, box-shadow .25s;
}
.beacon.lit { background: var(--accent); box-shadow: 0 0 0 8px rgba(245,166,35,.18), 0 0 18px rgba(245,166,35,.7); }

.track-btn {
  display: inline-flex; align-items: center; gap: .45rem;
  min-height: var(--tap); padding: .5rem .9rem;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--card); color: var(--text); font-weight: 600; font-size: .9rem;
  cursor: pointer;
}
.track-btn .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.track-btn[aria-pressed="true"] { border-color: var(--ok); color: var(--ok); }
.track-btn[aria-pressed="true"] .dot { background: var(--ok); animation: pulse 1.8s infinite; }
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: .35 } }

.tabs {
  position: sticky; top: calc(env(safe-area-inset-top) + 3.3rem); z-index: 15;
  display: flex; gap: .25rem; padding: .45rem .7rem;
  background: var(--bg); border-bottom: 1px solid var(--line);
  overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto; min-height: 38px; padding: .45rem .8rem;
  border: 1px solid transparent; border-radius: 999px;
  background: transparent; color: var(--muted); font: inherit; font-size: .9rem; cursor: pointer;
}
.tab.active { background: var(--card); color: var(--text); border-color: var(--line); }

main { padding: .9rem .9rem 2.5rem; max-width: 780px; margin: 0 auto; }
.panel { display: none; }
.panel.active { display: block; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px) } to { opacity: 1; transform: none } }

.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: .95rem; margin-bottom: .9rem;
}

.toolbar { display: flex; align-items: center; gap: .6rem; margin-bottom: .8rem; flex-wrap: wrap; }
.toolbar h2 { margin: 0; }
.spacer { flex: 1; }
.hint { font-size: .8rem; color: var(--muted); }

button { font: inherit; }
.primary, .ghost {
  min-height: var(--tap); padding: .55rem 1rem; border-radius: 10px;
  cursor: pointer; border: 1px solid var(--line); background: var(--card); color: var(--text);
  font-weight: 600; font-size: .92rem;
}
.primary { background: var(--accent); color: #22180a; border-color: var(--accent); }
.ghost:active, .primary:active { transform: translateY(1px); }
.ghost.danger { color: var(--danger); }
.row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-top: .6rem; }
.row.two { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }
.row.end { justify-content: flex-end; margin-top: 1rem; }

.field { display: block; margin-bottom: .75rem; }
.field > span { display: block; font-size: .8rem; color: var(--muted); margin-bottom: .3rem; }
input[type=text], input[type=number], input[type=password], input[type=time], select {
  width: 100%; min-height: var(--tap); padding: .55rem .7rem;
  background: #0e1626; color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; font: inherit; font-size: 1rem;
}
input[type=range] { width: 100%; accent-color: var(--accent); }
input[type=color] { width: 100%; height: var(--tap); padding: .2rem; background: #0e1626; border: 1px solid var(--line); border-radius: 10px; }
input:focus, select:focus { outline: 2px solid var(--accent-2); outline-offset: 1px; }

.zone-list { display: grid; gap: .7rem; }
.zone {
  display: grid; grid-template-columns: auto 1fr auto; gap: .8rem; align-items: center;
  background: var(--card); border: 1px solid var(--line); border-left: 4px solid var(--zone-color, var(--accent));
  border-radius: var(--radius); padding: .8rem .9rem;
}
.zone .zi { font-size: 1.5rem; line-height: 1; }
.zone .zname { font-weight: 640; }
.zone .zmeta { font-size: .78rem; color: var(--muted); margin-top: .15rem; }
.zone .zmeta b { color: var(--accent-2); font-weight: 600; }
.zone .zact { font-size: .74rem; color: var(--muted); margin-top: .25rem; }
.switch { position: relative; width: 48px; height: 28px; flex: 0 0 auto; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch span { position: absolute; inset: 0; background: #2a3652; border-radius: 999px; transition: .2s; }
.switch span::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px;
  background: #8fa0bd; border-radius: 50%; transition: .2s;
}
.switch input:checked + span { background: rgba(62,207,142,.3); }
.switch input:checked + span::after { transform: translateX(20px); background: var(--ok); }

.empty { text-align: center; padding: 2rem 1rem; color: var(--muted); border: 1px dashed var(--line); border-radius: var(--radius); }
.empty .big { color: var(--text); font-weight: 650; margin-bottom: .4rem; }

.readout {
  font: 600 1rem/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  background: #0e1626; border: 1px solid var(--line); border-radius: 10px;
  padding: .7rem .8rem; margin-bottom: .6rem; word-break: break-word;
}
.note { font-size: .8rem; color: var(--muted); margin: .6rem 0 0; }
.code { font: .78rem/1.5 ui-monospace, Menlo, monospace; background: #0e1626; border: 1px solid var(--line); border-radius: 10px; padding: .7rem; overflow-x: auto; white-space: pre-wrap; }

.live-zones { display: grid; gap: .45rem; }
.live-row { display: flex; align-items: center; gap: .6rem; padding: .5rem .2rem; border-bottom: 1px solid var(--line); font-size: .9rem; }
.live-row:last-child { border-bottom: none; }
.live-row .state { margin-left: auto; font-size: .78rem; color: var(--muted); }
.live-row.inside .state { color: var(--ok); font-weight: 650; }

.visit-list { display: grid; gap: .45rem; }
.visit {
  display: grid; grid-template-columns: auto 1fr; gap: .7rem; align-items: baseline;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: .6rem .8rem;
}
.visit .vev { font-size: .72rem; font-weight: 700; letter-spacing: .4px; text-transform: uppercase; }
.visit .vev.enter { color: var(--ok); }
.visit .vev.exit { color: var(--muted); }
.visit .vwhen { font-size: .78rem; color: var(--muted); }
.visit .vzone { font-weight: 600; }
.visit .vnote { font-size: .8rem; color: var(--muted); grid-column: 2; }

dialog {
  border: 1px solid var(--line); border-radius: 16px; background: var(--bg-2); color: var(--text);
  padding: 1rem; width: min(560px, 94vw); max-height: 90vh; overflow-y: auto;
}
dialog::backdrop { background: rgba(3,7,15,.7); }

.sched { border: 1px solid var(--line); border-radius: 10px; padding: .6rem .7rem; margin-bottom: .8rem; }
.sched summary { cursor: pointer; font-size: .85rem; color: var(--muted); }
.days { display: flex; gap: .3rem; flex-wrap: wrap; margin-top: .5rem; }
.day {
  min-width: 38px; min-height: 38px; border-radius: 8px; border: 1px solid var(--line);
  background: #0e1626; color: var(--muted); font-size: .8rem; cursor: pointer;
}
.day.on { background: var(--accent); color: #22180a; border-color: var(--accent); font-weight: 700; }

.actions { border: 1px solid var(--line); border-radius: 10px; padding: .7rem .8rem; margin: 0 0 .8rem; }
.actions legend { font-size: .8rem; color: var(--muted); padding: 0 .3rem; }
.action-list { display: grid; gap: .4rem; margin-bottom: .6rem; }
.action {
  display: flex; align-items: center; gap: .5rem;
  background: #0e1626; border: 1px solid var(--line); border-radius: 8px; padding: .45rem .6rem; font-size: .85rem;
}
.action .atype { font-weight: 640; }
.action .atarget { color: var(--muted); font-size: .78rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.action .adel { margin-left: auto; background: none; border: none; color: var(--danger); font-size: 1.1rem; cursor: pointer; padding: .2rem .35rem; }

.map-wrap { display: grid; gap: .6rem; }
.map-head { display: flex; align-items: center; justify-content: space-between; }
.map { height: 52vh; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); background: #0e1626; }
#mapDialog { width: min(680px, 96vw); }

.checklist { list-style: none; padding: 0; margin: 0 0 .6rem; }
.checklist li { padding: .45rem 0; border-bottom: 1px solid var(--line); font-size: .9rem; }
.checklist li.done { color: var(--muted); text-decoration: line-through; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 1rem); z-index: 60;
  background: #1d2a44; border: 1px solid var(--line); border-radius: 12px;
  padding: .7rem 1rem; font-size: .88rem; max-width: 90vw;
  box-shadow: 0 10px 30px rgba(0,0,0,.5);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fa; --bg-2: #fff; --card: #fff; --line: #dfe5ef;
    --text: #16203a; --muted: #5b6a85;
  }
  input[type=text], input[type=number], input[type=password], input[type=time], select,
  .readout, .code, .action, .day, .switch span { background: #f7f9fc; }
  .switch span { background: #d3dbe8; }
}
