:root {
  --bg: #0b0d10;
  --card: #15181d;
  --card-2: #1b1f26;
  --line: rgba(255, 255, 255, 0.07);
  --text: #f2f4f7;
  --muted: #8b939f;
  --green: #2fd27a;
  --amber: #f5b544;
  --red: #ff5d5d;
  --accent: #2fd27a;
  --accent-ink: #062314;
  --radius: 20px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
}

main {
  max-width: 480px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px max(28px, env(safe-area-inset-bottom));
  animation: rise 0.45s var(--ease) both;
}

@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

/* ---------- buttons */
.btn {
  appearance: none;
  border: 0;
  border-radius: 14px;
  height: 48px;
  padding: 0 20px;
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: var(--card-2);
  cursor: pointer;
  transition: transform 0.15s var(--ease), opacity 0.2s, background 0.2s;
  width: 100%;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.big { height: 56px; font-size: 17px; border-radius: 16px; margin: 14px 0 6px; }
.btn.quiet { background: var(--card-2); color: var(--text); }
.btn[disabled], .btn.busy { opacity: 0.55; pointer-events: none; }

.icon-btn {
  appearance: none;
  border: 0;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--card);
  color: var(--text);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.2s, transform 0.15s var(--ease);
}
.icon-btn:active { transform: scale(0.94); }
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; stroke: currentColor; stroke-width: 0; }
#sheet-close svg { fill: none; stroke-width: 2; stroke-linecap: round; }

.link-btn {
  appearance: none;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 15px;
  padding: 10px;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- sign in */
.signin { display: flex; flex-direction: column; justify-content: center; min-height: 100dvh; }
.brand { text-align: center; margin-bottom: 28px; }
.brand-mark {
  width: 64px; height: 64px; margin: 0 auto 14px; border-radius: 18px;
  background: var(--card); border: 1px solid var(--line);
  display: grid; place-items: center;
}
.brand-mark svg { width: 34px; height: 34px; fill: var(--green); }
.brand h1 { margin: 0; font-size: 24px; letter-spacing: -0.02em; }
.brand p { margin: 4px 0 0; color: var(--muted); }

.form { padding: 20px; display: grid; gap: 14px; }
.form label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.form input {
  height: 50px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  padding: 0 14px;
  font: inherit;
  font-size: 16px; /* no zoom on iOS */
  transition: border-color 0.2s;
}
.form input:focus { border-color: rgba(47, 210, 122, 0.6); outline: none; }
.form-error { min-height: 20px; margin: 0; color: var(--red); font-size: 14px; }
.form-error:empty { min-height: 0; display: none; }

/* ---------- home */
.top { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 16px; }
.eyebrow { color: var(--muted); font-size: 13px; }
.title { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.15; }

.menu-wrap { position: relative; }
.menu {
  position: absolute;
  right: 0;
  top: 52px;
  min-width: 190px;
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 6px;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.45);
  z-index: 20;
  transform-origin: top right;
  animation: pop 0.18s var(--ease) both;
}
@keyframes pop { from { opacity: 0; transform: scale(0.94); } to { opacity: 1; transform: none; } }
.menu button {
  display: block;
  width: 100%;
  text-align: left;
  appearance: none;
  border: 0;
  background: none;
  color: var(--text);
  font: inherit;
  padding: 12px 14px;
  border-radius: 10px;
  cursor: pointer;
}
.menu button:hover, .menu button:active { background: rgba(255, 255, 255, 0.06); }
.menu .danger { color: var(--red); }

.status { padding: 22px 20px 20px; position: relative; overflow: hidden; }
.status::before {
  content: "";
  position: absolute;
  inset: -40% -20% auto auto;
  width: 220px;
  height: 220px;
  border-radius: 50%;
  background: radial-gradient(closest-side, var(--glow, transparent), transparent);
  opacity: 0.35;
  transition: background 0.4s;
  pointer-events: none;
}
.status-row { display: flex; align-items: center; gap: 12px; }
.dot {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--dot, var(--muted));
  box-shadow: 0 0 0 0 var(--dot, transparent);
  flex: none;
  transition: background 0.3s;
}
.state { font-size: 30px; font-weight: 700; letter-spacing: -0.02em; }
.status-meta { margin-top: 8px; color: var(--muted); font-size: 15px; display: flex; flex-wrap: wrap; gap: 6px; }
.status-meta #number { color: var(--text); font-variant-numeric: tabular-nums; direction: ltr; }
.status-note { margin-top: 12px; font-size: 14px; color: var(--muted); padding-top: 12px; border-top: 1px solid var(--line); }

.status[data-state="open"] { --dot: var(--green); --glow: var(--green); }
.status[data-state="connecting"] { --dot: var(--amber); --glow: var(--amber); }
.status[data-state="close"] { --dot: var(--red); --glow: var(--red); }
.status[data-state="unknown"] { --dot: var(--muted); }
.status[data-state="open"] .dot { animation: pulse 2.4s ease-out infinite; }
.status[data-state="connecting"] .dot { animation: blink 1.1s ease-in-out infinite; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(47, 210, 122, 0.55); }
  70% { box-shadow: 0 0 0 12px rgba(47, 210, 122, 0); }
  100% { box-shadow: 0 0 0 0 rgba(47, 210, 122, 0); }
}
@keyframes blink { 50% { opacity: 0.35; } }

#relink-btn.calm { background: var(--card); color: var(--muted); border: 1px solid var(--line); }

.block { margin-top: 22px; }
.block h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 0 4px 8px; font-weight: 600; }

.list { list-style: none; margin: 0; padding: 4px 0; }
.list li { display: flex; gap: 12px; align-items: flex-start; padding: 11px 16px; }
.list li + li { border-top: 1px solid var(--line); }
.list .empty { color: var(--muted); font-size: 15px; justify-content: center; padding: 16px; }
.list .grow { flex: 1; min-width: 0; }
.list .line1 { display: flex; justify-content: space-between; gap: 10px; font-size: 15px; }
.list .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .when { color: var(--muted); font-size: 13px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.list .text {
  color: var(--muted); font-size: 14px; margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  unicode-bidi: plaintext;
}
.dir {
  width: 28px; height: 28px; border-radius: 50%; flex: none; margin-top: 1px;
  display: grid; place-items: center; font-size: 14px; font-weight: 700;
}
.dir.in { background: rgba(120, 160, 255, 0.14); color: #9db8ff; }
.dir.out { background: rgba(47, 210, 122, 0.14); color: var(--green); }
.tag {
  font-size: 11px; font-weight: 600; color: var(--muted); border: 1px solid var(--line);
  border-radius: 6px; padding: 0 5px; margin-left: 6px; vertical-align: 1px;
}
.alert-dot { width: 8px; height: 8px; border-radius: 50%; margin-top: 7px; flex: none; background: var(--muted); }
.alert-dot.open { background: var(--green); }
.alert-dot.connecting { background: var(--amber); }
.alert-dot.close { background: var(--red); }

.foot { text-align: center; color: var(--muted); font-size: 12px; margin-top: 20px; }

/* ---------- sheet */
.scrim {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  z-index: 30; opacity: 0; transition: opacity 0.25s;
}
.scrim.on { opacity: 1; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0;
  max-width: 520px; margin: 0 auto;
  background: var(--card);
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 26px 26px 0 0;
  padding: 8px 20px max(24px, env(safe-area-inset-bottom));
  z-index: 40;
  transform: translateY(100%);
  transition: transform 0.38s var(--ease);
  max-height: 92dvh;
  overflow-y: auto;
}
.sheet.on { transform: none; }
.grip { width: 38px; height: 5px; border-radius: 3px; background: rgba(255, 255, 255, 0.18); margin: 0 auto 6px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head h2 { margin: 0; font-size: 20px; }
.sheet-head .icon-btn { background: var(--card-2); width: 36px; height: 36px; }
.sheet-head .icon-btn svg { width: 18px; height: 18px; }
.sheet-body { padding: 18px 0 4px; text-align: center; animation: rise 0.3s var(--ease) both; }
.muted { color: var(--muted); }
.sheet-error { color: var(--text); margin: 6px 0 18px; }

.spinner {
  width: 34px; height: 34px; margin: 18px auto 12px; border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.12); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.code-wrap { display: flex; align-items: center; justify-content: center; gap: 14px; margin: 4px 0 6px; }
.code {
  font: 700 38px/1.1 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  letter-spacing: 0.06em;
  font-variant-numeric: tabular-nums;
  user-select: all;
  transition: opacity 0.3s;
}
.code.stale { opacity: 0.3; text-decoration: line-through; text-decoration-thickness: 2px; }
.ring { position: relative; width: 44px; height: 44px; flex: none; }
.ring svg { width: 44px; height: 44px; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 4; }
.ring-bg { stroke: rgba(255, 255, 255, 0.1); }
.ring-fg { stroke: var(--accent); stroke-linecap: round; stroke-dasharray: 119.4; transition: stroke-dashoffset 1s linear, stroke 0.3s; }
.ring.low .ring-fg { stroke: var(--amber); }
.ring span { position: absolute; inset: 0; display: grid; place-items: center; font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }

.qr { margin: 14px auto 4px; width: 220px; height: 220px; padding: 10px; background: #fff; border-radius: 16px; }
.qr img { width: 100%; height: 100%; display: block; image-rendering: pixelated; }
.steps { color: var(--muted); font-size: 14px; margin: 14px 0 16px; }
.sheet-actions { display: grid; gap: 4px; }

/* ---------- toast */
.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  background: #f2f4f7; color: #0b0d10; font-weight: 600; font-size: 15px;
  padding: 12px 18px; border-radius: 999px; opacity: 0; pointer-events: none;
  transition: opacity 0.25s, transform 0.3s var(--ease);
  z-index: 60; max-width: calc(100% - 32px); text-align: center;
}
.toast.on { opacity: 1; transform: translate(-50%, 0); }

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