:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --text: #16202a;
  --muted: #5b6875;
  --line: #dde3e9;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #e0f2ef;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn: #9a6700;
  --warn-soft: #fff4d6;
  --ok: #1a7f37;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Ubuntu, sans-serif;
  --mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1418;
    --card: #162027;
    --text: #e8eef2;
    --muted: #9aa8b3;
    --line: #26343e;
    --accent: #2bb3a3;
    --accent-ink: #062521;
    --accent-soft: #123a36;
    --danger: #ff7b72;
    --danger-soft: #3a1d1b;
    --warn: #e3b341;
    --warn-soft: #3a2f12;
    --ok: #56d364;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}

button, input, textarea, select { font: inherit; color: inherit; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.1rem; }
.logo { width: 28px; height: 28px; }

.status { display: inline-flex; align-items: center; gap: 8px; font-size: 0.875rem; color: var(--muted); }
.status .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.status.online .dot { background: var(--ok); }
.status.connecting .dot { background: var(--warn); animation: pulse 1.2s infinite; }
.status.offline .dot, .status.replaced .dot { background: var(--danger); }
@keyframes pulse { 50% { opacity: 0.3; } }

.container { max-width: 880px; margin: 0 auto; padding: 16px; display: grid; gap: 16px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.card h2 { margin: 0 0 6px; font-size: 1.15rem; }
.card h3 { margin: 0 0 6px; font-size: 1rem; }
.card > p:first-of-type { margin-top: 0; }
.center { text-align: center; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.hidden { display: none !important; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.grow { flex: 1; min-width: 160px; }
.row.between { justify-content: space-between; }
.stack { display: grid; gap: 10px; }
.grid2 { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }

label { font-weight: 600; font-size: 0.9rem; }
input[type="text"], textarea {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  outline: none;
}
input[type="text"]:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; min-height: 96px; }
input.code { font-family: var(--mono); letter-spacing: 0.12em; text-transform: uppercase; font-size: 1.15rem; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--card);
  cursor: pointer;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.small { min-height: 34px; padding: 5px 10px; font-size: 0.875rem; }
.btn.block { width: 100%; }
.btn.ghost { background: transparent; border-color: transparent; }

.tabs { display: flex; gap: 4px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 4px; }
.tabs button {
  flex: 1;
  min-height: 42px;
  border: 0;
  background: transparent;
  border-radius: 9px;
  font-weight: 600;
  cursor: pointer;
  color: var(--muted);
}
.tabs button[aria-selected="true"] { background: var(--accent-soft); color: var(--text); }
.badge { display: inline-block; font-size: 0.75rem; font-weight: 700; padding: 1px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--text); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.muted { background: var(--bg); color: var(--muted); border: 1px solid var(--line); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 40px; padding: 6px 12px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--bg); cursor: pointer; font-weight: 600;
}
.chip[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }
.chip .on, .dev .on { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; }
.chip .on.yes, .dev .on.yes { background: var(--ok); }

.drop {
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  padding: 22px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.drop:hover, .drop.over { border-color: var(--accent); background: var(--accent-soft); }
.drop strong { display: block; font-size: 1.05rem; }

.list { display: grid; gap: 10px; }
.item {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  display: grid;
  gap: 8px;
  background: var(--card);
}
.item .top { display: flex; gap: 10px; align-items: flex-start; }
.item .icon { font-size: 1.3rem; line-height: 1.2; flex: none; width: 28px; text-align: center; }
.item .main { flex: 1; min-width: 0; }
.item .name { font-weight: 600; overflow-wrap: anywhere; }
.item .meta { font-size: 0.85rem; color: var(--muted); }
.item .actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.item pre.text {
  margin: 0;
  padding: 10px;
  border-radius: 8px;
  background: var(--bg);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font: 0.92rem/1.45 var(--mono);
  max-height: 220px;
  overflow: auto;
}
.item.error { border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }

progress {
  width: 100%;
  height: 8px;
  appearance: none;
  border: 0;
  border-radius: 999px;
  background: var(--bg);
  overflow: hidden;
}
progress::-webkit-progress-bar { background: var(--bg); border-radius: 999px; }
progress::-webkit-progress-value { background: var(--accent); border-radius: 999px; transition: width 0.2s; }
progress::-moz-progress-bar { background: var(--accent); border-radius: 999px; }

.dev { display: flex; gap: 12px; align-items: flex-start; }
.dev .on { margin-top: 8px; }
.dev .body { flex: 1; min-width: 0; }
.fp { font-family: var(--mono); font-size: 0.85rem; color: var(--muted); letter-spacing: 0.04em; }

.notice { padding: 12px 14px; border-radius: 10px; background: var(--warn-soft); color: var(--text); border: 1px solid color-mix(in srgb, var(--warn) 30%, transparent); }
.notice.info { background: var(--accent-soft); border-color: transparent; }
.notice.danger { background: var(--danger-soft); border-color: transparent; }

.option { display: grid; gap: 10px; align-content: start; }
.option .lead { font-size: 1.6rem; }

/* Modal */
.backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(8, 12, 16, 0.55);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.modal {
  width: min(520px, 100%);
  max-height: calc(100vh - 32px);
  overflow: auto;
  background: var(--card);
  border-radius: 18px;
  padding: 22px;
  border: 1px solid var(--line);
  display: grid;
  gap: 14px;
}
.modal h2 { margin: 0; font-size: 1.2rem; }
.qr { display: flex; justify-content: center; }
.qr svg { width: min(260px, 70vw); height: auto; background: #fff; border-radius: 12px; padding: 10px; }
.bigcode { font: 700 clamp(1.6rem, 7vw, 2.2rem)/1.2 var(--mono); letter-spacing: 0.14em; text-align: center; overflow-wrap: anywhere; }
.recovery { font: 600 1.02rem/1.7 var(--mono); letter-spacing: 0.06em; text-align: center; background: var(--bg); padding: 12px; border-radius: 10px; word-spacing: 0.3em; overflow-wrap: anywhere; user-select: all; }
.sas { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 4px; }
.sas figure { margin: 0; display: grid; justify-items: center; gap: 2px; min-width: 0; }
.sas .e { font-size: clamp(1.8rem, 9vw, 2.4rem); line-height: 1.1; }
.sas figcaption { font-size: 0.72rem; color: var(--muted); text-align: center; overflow-wrap: anywhere; }
.digits { text-align: center; font: 700 1.6rem var(--mono); letter-spacing: 0.15em; }
video.scan { width: 100%; border-radius: 12px; background: #000; }

.toasts { position: fixed; z-index: 60; left: 50%; transform: translateX(-50%); bottom: calc(16px + env(safe-area-inset-bottom)); display: grid; gap: 8px; width: min(440px, calc(100% - 24px)); }
.toast { background: var(--text); color: var(--bg); padding: 12px 14px; border-radius: 12px; box-shadow: 0 6px 24px rgba(0,0,0,0.2); font-size: 0.95rem; animation: toast-in 0.18s ease-out; }
.toast.error { background: var(--danger); color: #fff; }
.toast.warn { background: var(--warn); color: #fff; }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }

.empty { text-align: center; color: var(--muted); padding: 18px 8px; }
details summary { cursor: pointer; font-weight: 600; }
.switch { display: flex; gap: 10px; align-items: center; font-weight: 500; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); }

@media (max-width: 560px) {
  .container { padding: 12px; gap: 12px; }
  .card { padding: 14px; }
  .item .top { flex-wrap: wrap; }
  .item .actions { width: 100%; justify-content: stretch; }
  .item .actions .btn { flex: 1; }
  .modal { padding: 18px; }
}

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