/* ── Reset / base ──────────────────────────────────────────────────────────── */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* Kill iOS double-tap-to-zoom on controls (esp. important since retapping a
   busy button is the natural gesture and must not zoom the page). */
button, input, select, textarea, .tab, .tab-add, .chat-item, .icon-btn { touch-action: manipulation; }
:root {
  --bg:        #0d1117;
  --bg2:       #131a24;
  --card:      #1a2230;
  --card2:     #202a3a;
  --line:      #2b3648;
  --text:      #e6edf3;
  --muted:     #8a97a8;
  --blue:      #2f81d6;
  --green:     #2ea043;
  --red:       #d1352c;
  --purple:    #8e44ad;
  --orange:    #e0821f;
  --gray:      #45505f;
  --radius:    14px;
  --tap:       46px;
}
html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  overscroll-behavior-y: none;
}
body { padding-bottom: env(safe-area-inset-bottom); }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.85rem; }

/* ── Header ────────────────────────────────────────────────────────────────── */
#topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 14px 10px;
  background: #0b0f16;
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 1.15rem; letter-spacing: 0.2px; }
.brand span { color: var(--text); }
.top-actions { display: flex; gap: 8px; }
.icon-btn {
  width: var(--tap); height: var(--tap);
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--card); color: var(--text);
  font-size: 1.2rem; cursor: pointer;
}
.icon-btn:active { background: var(--card2); }

/* ── Tabs ──────────────────────────────────────────────────────────────────── */
.tabs {
  position: sticky; top: calc(env(safe-area-inset-top) + 67px); z-index: 15;
  display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none;
  padding: 10px 14px; background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 6px;
  height: 40px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 20px;
  background: var(--card); color: var(--text);
  font-size: 0.9rem; font-weight: 600; white-space: nowrap; cursor: pointer;
}
.tab.active { border-color: var(--blue); background: #16283e; }
.tab .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--gray); }
.tab-add {
  flex: 0 0 auto; width: 40px; height: 40px;
  border: 1px dashed var(--line); border-radius: 20px;
  background: transparent; color: var(--muted); font-size: 1.2rem; cursor: pointer;
}

/* dot status colors */
.dot.on   { background: var(--green); box-shadow: 0 0 6px var(--green); }
.dot.busy { background: var(--orange); }
.dot.off  { background: var(--gray); }
.dot.run  { background: var(--green); animation: pulse 1.4s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.35; } }

/* ── Layout container ──────────────────────────────────────────────────────── */
#main, .logview { max-width: 720px; margin: 0 auto; padding: 14px; }

/* ── Cards / sections ──────────────────────────────────────────────────────── */
.card { display: flex; flex-direction: column; gap: 12px; }
.status-strip {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px;
  padding: 14px; border-radius: var(--radius);
  background: linear-gradient(180deg, #1c2636, #161e2b);
  border: 1px solid var(--line);
}
.status-strip .who { display: flex; align-items: center; gap: 8px; min-width: 0; }
.status-strip .who .name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.4px;
  padding: 3px 9px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted);
}
.badge.connected { color: var(--green); border-color: #1d5c2c; background: #12291a; }
.badge.code_sent, .badge.needs2fa { color: var(--orange); border-color: #5c451d; background: #291f12; }
.badge.idle { color: var(--muted); }
.badge.active { color: var(--green); border-color: #1d5c2c; background: #12291a; }
.counters { display: flex; gap: 14px; margin-left: auto; font-variant-numeric: tabular-nums; }
.counters b { color: var(--blue); }

details.sec {
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--card); overflow: hidden;
}
details.sec > summary {
  list-style: none; cursor: pointer; user-select: none;
  padding: 15px 16px; font-weight: 700; font-size: 0.98rem;
  display: flex; align-items: center; justify-content: space-between;
}
details.sec > summary::-webkit-details-marker { display: none; }
details.sec > summary::after { content: "▾"; color: var(--muted); transition: transform 0.2s; }
details.sec[open] > summary::after { transform: rotate(180deg); }
.sec-body { padding: 4px 16px 16px; display: flex; flex-direction: column; gap: 12px; }
.section-title { font-weight: 700; }
hr.sep { border: none; border-top: 1px solid var(--line); width: 100%; margin: 2px 0; }
code { background: var(--bg2); padding: 1px 6px; border-radius: 5px; font-size: 0.85em;
  font-family: ui-monospace, Menlo, Consolas, monospace; }
label.field .muted { font-weight: 400; }

/* ── Form controls ─────────────────────────────────────────────────────────── */
label.field { display: flex; flex-direction: column; gap: 5px; font-size: 0.82rem; color: var(--muted); }
input[type="text"], input[type="password"], input[type="tel"], input[type="number"], select, textarea {
  width: 100%; min-height: var(--tap);
  padding: 11px 13px; font-size: 16px;
  color: var(--text); background: var(--bg2);
  border: 1px solid var(--line); border-radius: 11px;
  appearance: none;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--blue); }
textarea { min-height: 64px; resize: vertical; line-height: 1.4; }
/* Restore a visible dropdown chevron (appearance:none removed the native one). */
select {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path fill='none' stroke='%238a97a8' stroke-width='1.6' d='M1 1.5l5 5 5-5'/></svg>");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 34px;
}

.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
/* Let inputs/selects shrink below their intrinsic width so input+button rows
   don't overflow (and get clipped by the section's overflow:hidden) on ~360px
   phones; keep the small paired buttons at their natural size. */
.row > input, .row > select, .row > textarea { min-width: 0; }
.row > .btn-sm { flex: 0 0 auto; }
.row.between { align-items: center; justify-content: space-between; }
.inline { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

/* ── Buttons ───────────────────────────────────────────────────────────────── */
.btn {
  min-height: var(--tap); padding: 0 16px;
  border: none; border-radius: 12px; cursor: pointer;
  font-size: 0.95rem; font-weight: 700; color: #fff;
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn:active:not(:disabled) { filter: brightness(1.12); }
.btn-lg { width: 100%; min-height: 52px; font-size: 1.02rem; }
.btn-sm { min-height: 38px; padding: 0 12px; font-size: 0.85rem; }
.btn-full { width: 100%; }
.btn-blue   { background: var(--blue); }
.btn-green  { background: var(--green); }
.btn-red    { background: var(--red); }
.btn-purple { background: var(--purple); }
.btn-orange { background: var(--orange); }
.btn-gray   { background: var(--gray); }
.btn-ghost  { background: transparent; border: 1px solid var(--line); color: var(--text); font-weight: 600; }

/* ── Switch ────────────────────────────────────────────────────────────────── */
.switch { position: relative; display: inline-block; width: 52px; height: 30px; flex: 0 0 auto; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider {
  position: absolute; inset: 0; background: var(--gray); border-radius: 999px; transition: 0.2s;
}
.switch .slider::before {
  content: ""; position: absolute; height: 24px; width: 24px; left: 3px; top: 3px;
  background: #fff; border-radius: 50%; transition: 0.2s;
}
.switch input:checked + .slider { background: var(--green); }
.switch input:checked + .slider::before { transform: translateX(22px); }

/* ── Chat picker ───────────────────────────────────────────────────────────── */
.chatlist {
  border: 1px solid var(--line); border-radius: 11px; background: var(--bg2);
  max-height: 46vh; overflow-y: auto; -webkit-overflow-scrolling: touch;
}
.chat-item {
  padding: 11px 13px; border-bottom: 1px solid #1c2534; cursor: pointer;
  display: flex; flex-direction: column; gap: 2px;
}
.chat-item:last-child { border-bottom: none; }
.chat-item.sel { background: #1a3350; }
.chat-item .cname { font-weight: 600; font-size: 0.92rem; }
.chat-item .cmeta { font-size: 0.74rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.pick-count { font-size: 0.78rem; color: var(--muted); }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 11px; border-radius: 999px; font-size: 0.8rem; font-weight: 600;
  background: var(--card2); border: 1px solid var(--line); max-width: 100%;
}
.chip .lbl { color: var(--muted); }
.chip .val { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip.src { border-color: #1c4368; color: var(--blue); }
.chip.dst { border-color: #1d5c2c; color: var(--green); }
.chip.src .val, .chip.dst .val { color: inherit; }

/* ── Routes ────────────────────────────────────────────────────────────────── */
.routes { display: flex; flex-direction: column; gap: 8px; }
.route {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 12px; border: 1px solid var(--line); border-radius: 11px; background: var(--bg2);
}
.route .num { color: var(--muted); font-weight: 700; font-variant-numeric: tabular-nums; }
.route .rbody { flex: 1; min-width: 0; font-size: 0.86rem; }
.route .rbody .r-src { font-weight: 600; }
.route .rbody .r-dst { color: var(--muted); }
.route .r-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; }
.route .chain { color: var(--orange); font-size: 0.75rem; }
.rdel {
  flex: 0 0 auto; width: var(--tap); height: var(--tap); border-radius: 9px;
  background: #2a1518; color: var(--red); border: 1px solid #4a2226; font-size: 1rem; cursor: pointer;
}
.rgear {
  flex: 0 0 auto; width: var(--tap); height: var(--tap); border-radius: 9px;
  background: var(--card2); color: var(--text); border: 1px solid var(--line); font-size: 1rem; cursor: pointer;
}
.rules-badge {
  font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.3px;
  color: var(--purple); border: 1px solid #3d2a52; background: #211830;
  padding: 1px 6px; border-radius: 999px; vertical-align: middle;
}

/* ── Route cards (many-to-many) ────────────────────────────────────────────── */
.route-card {
  display: flex; flex-direction: column; gap: 10px;
  padding: 12px; border: 1px solid var(--line); border-radius: 11px; background: var(--bg2);
}
.route-card.incomplete { border-color: #5c451d; }
.route-head { display: flex; align-items: center; gap: 8px; }
.route-head .num { color: var(--muted); font-weight: 700; font-variant-numeric: tabular-nums; }
.route-name {
  flex: 1; min-width: 0; min-height: 38px !important;
  padding: 7px 11px !important; font-size: 0.92rem !important; font-weight: 600;
  /* This input has no type attribute, so the input[type="text"] block above
     does NOT match it — set the dark-theme colors explicitly or the browser
     default (black on dark) makes the nickname unreadable. */
  color: var(--text); background: transparent !important;
  border: 1px dashed var(--line) !important; border-radius: 11px;
  font-family: inherit;
}
.route-name::placeholder { color: var(--muted); font-weight: 400; }
.route-name:focus { border-style: solid !important; border-color: var(--blue) !important; outline: none; }
.route-flow { display: flex; flex-direction: column; gap: 10px; }
.route-side { display: flex; flex-direction: column; gap: 6px; }
.side-title { font-size: 0.74rem; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.4px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.chip .chain { color: var(--orange); }
.chip-x {
  border: none; background: transparent; color: var(--muted);
  font-size: 0.85rem; cursor: pointer; padding: 2px 2px 2px 4px; line-height: 1;
}
.chip-x:active { color: var(--red); }
.chip.add {
  border: 1px dashed var(--line); background: transparent; color: var(--muted);
  cursor: pointer; min-height: 32px;
}
.chip.add:active { color: var(--text); border-color: var(--blue); }
.route-meta { display: flex; flex-wrap: wrap; gap: 6px 12px; font-size: 0.78rem; color: var(--muted); }
.route-meta .warn { color: var(--orange); }

/* Weekday selector + autosave status (rules editor) */
.days { display: flex; flex-wrap: wrap; gap: 6px; }
.days .day {
  min-height: 36px; padding: 0 12px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--line); background: var(--bg2); color: var(--muted);
  font-size: 0.82rem; font-weight: 700;
}
.days .day.on { color: #fff; background: var(--blue); border-color: var(--blue); }
.rule-status { margin-left: auto; font-size: 0.75rem; color: var(--muted); white-space: nowrap; }
.rule-status.ok { color: var(--green); }
.rule-status.err { color: var(--red); }
.rule-status.busy { color: var(--orange); }
.userchip {
  display: inline-flex; align-items: center;
  padding: 0 12px; min-height: var(--tap); border-radius: 12px;
  border: 1px solid var(--line); background: var(--card);
  font-size: 0.82rem; font-weight: 700; color: var(--muted);
}

/* ── Admin panel: invites + users ──────────────────────────────────────────── */
.invite {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 11px; margin-bottom: 6px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg2);
}
.invite.used { opacity: 0.6; }
.invite.fresh { border-color: var(--blue); background: #16283e; }
.inv-code {
  display: inline-flex; align-items: center; gap: 6px; flex: 0 0 auto;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.95rem; font-weight: 700; letter-spacing: 1px;
  color: var(--blue); background: #16283e;
  border: 1px solid #1c4368; border-radius: 9px;
  padding: 7px 10px; min-height: 38px; cursor: pointer;
  user-select: all;            /* long-press can select the code as a fallback */
}
.inv-code:active { filter: brightness(1.3); }
.inv-code:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }
.inv-code.copied { color: var(--green); background: #12291a; border-color: #1d5c2c; }
.copy-ico { font-size: 0.78rem; opacity: 0.75; letter-spacing: 0; }
.invite.used .inv-code {
  color: var(--muted); background: transparent; border-color: var(--line);
  text-decoration: line-through; cursor: default; user-select: text;
}
.inv-meta { flex: 1; min-width: 0; font-size: 0.8rem; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* user rows (same shell as .invite, but wraps so the role controls fit) */
.urow {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 9px 11px; margin-bottom: 6px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg2);
}
.urow .inv-meta { flex: 1 1 140px; }
.urow .btn-sm { flex: 0 0 auto; }
.role-badge {
  flex: 0 0 auto;
  font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.4px;
  padding: 2px 8px; border-radius: 999px;
  color: var(--muted); border: 1px solid var(--line); background: transparent;
}
.role-badge.admin { color: var(--purple); border-color: #3d2a52; background: #211830; }

/* ── Per-route rules editor ────────────────────────────────────────────────── */
.routeedit { max-width: 720px; margin: 0 auto; padding: 14px; }
.editor-top {
  display: flex; align-items: center; gap: 10px; margin-bottom: 12px;
  position: sticky; top: calc(env(safe-area-inset-top) + 67px); z-index: 10;
  background: var(--bg); padding: 6px 0;
}
.editor-title { font-weight: 700; font-size: 0.95rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.routeedit .card { border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); }
.empty { color: var(--muted); font-size: 0.86rem; text-align: center; padding: 10px; }

/* ── Log ───────────────────────────────────────────────────────────────────── */
.logbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.log {
  margin: 0; padding: 12px; border: 1px solid var(--line); border-radius: var(--radius);
  background: #0a0e14; color: #b9c6d6;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px; line-height: 1.5; white-space: pre-wrap; word-break: break-word;
  height: 70vh; overflow-y: auto; -webkit-overflow-scrolling: touch;
}

/* ── Auth overlay ──────────────────────────────────────────────────────────── */
.overlay {
  position: fixed; inset: 0; z-index: 100;
  background: var(--bg); display: flex; align-items: center; justify-content: center; padding: 24px;
}
.auth-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; text-align: center; }
.auth-logo { font-size: 3rem; }
.auth-card h1 { margin: 0; font-size: 1.4rem; }
.auth-err { color: var(--red); font-size: 0.85rem; min-height: 1.2em; }

/* ── Toast ─────────────────────────────────────────────────────────────────── */
.toast {
  /* Top-anchored so the on-screen keyboard (up throughout the login flow)
     never hides error/status messages. */
  position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 120px);
  transform: translateX(-50%); z-index: 200;
  max-width: 90vw; padding: 12px 18px; border-radius: 12px;
  background: #222d3d; color: var(--text); border: 1px solid var(--line);
  font-size: 0.9rem; box-shadow: 0 8px 30px rgba(0,0,0,0.5);
}
.toast.err { border-color: #5a2429; background: #2a1518; }
.toast.ok  { border-color: #1d5c2c; background: #12291a; }

/* ── Desktop niceties ──────────────────────────────────────────────────────── */
@media (min-width: 760px) {
  body { font-size: 15px; }
  .chatlist { max-height: 380px; }
}
