:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #f0efeb;
  --text: #1c1b19;
  --muted: #6b6862;
  --border: #e4e2dc;
  --accent: #1f6f4a;
  --accent-text: #ffffff;
  --accent-soft: #e3f1e9;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn-soft: #fff4dc;
  --warn-text: #7a4b00;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 4px 16px rgb(0 0 0 / 0.05);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151515;
    --surface: #1e1e1d;
    --surface-2: #272725;
    --text: #f1efea;
    --muted: #a19d95;
    --border: #33322f;
    --accent: #4cc38a;
    --accent-text: #0b1f15;
    --accent-soft: #1b3326;
    --danger: #ff8a7a;
    --danger-soft: #3a1d1a;
    --warn-soft: #3a2f16;
    --warn-text: #ffd27a;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3);
    color-scheme: dark;
  }
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2 { margin: 0; line-height: 1.25; letter-spacing: -0.01em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.15rem; }
p { margin: 0; }
.muted { color: var(--muted); font-size: 0.93rem; }
code { font: 0.9em ui-monospace, SFMono-Regular, Menlo, monospace; }

/* ---------- brand */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 1.05rem; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 8px; background: var(--accent);
  position: relative; flex: none;
}
.brand-mark::after {
  content: ""; position: absolute; left: 8px; top: 5px; width: 10px; height: 16px;
  border: 2px solid var(--accent-text); border-bottom: none; border-radius: 3px 3px 0 0;
}

/* ---------- controls */
label, legend { display: block; font-size: 0.88rem; font-weight: 600; margin: 18px 0 6px; padding: 0; }
input:not([type="radio"]) {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 11px 13px;
  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(--muted); opacity: 0.7; }
fieldset { border: 0; margin: 18px 0 0; padding: 0; min-width: 0; }
legend { margin-top: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; font-size: 0.95rem; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 9px 16px; cursor: pointer; white-space: nowrap;
  transition: background-color 0.15s, transform 0.1s, opacity 0.15s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn:focus-visible, .menu-item:focus-visible, .icon-btn:focus-visible { outline: 3px solid var(--accent-soft); outline-offset: 1px; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.wide { width: 100%; padding: 12px; margin-top: 10px; }
.btn.icon { padding: 9px 12px; font-size: 1.1rem; line-height: 1; }

.error { color: var(--danger); font-size: 0.9rem; min-height: 1.4em; margin-top: 8px; }

.segmented { display: flex; gap: 6px; }
.segmented.wrap { flex-wrap: wrap; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label {
  margin: 0; font-weight: 500; font-size: 0.9rem; padding: 7px 13px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s;
}
.segmented input:checked + label { background: var(--accent-soft); border-color: var(--accent); color: var(--text); }
.segmented input:focus-visible + label { box-shadow: 0 0 0 3px var(--accent-soft); }
.segmented + input { margin-top: 10px; }

/* ---------- sign in */
.auth { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px; padding: 28px 24px; box-shadow: var(--shadow);
}
.auth-card .brand { margin-bottom: 28px; }
.auth-card h1 + .muted { margin-top: 6px; }
.code-input { font: 600 1.3rem ui-monospace, SFMono-Regular, Menlo, monospace !important; letter-spacing: 0.3em; }
.otp { font-size: 1.6rem !important; letter-spacing: 0.4em; text-align: center; font-variant-numeric: tabular-nums; }

/* ---------- app shell */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; justify-content: space-between; align-items: center;
  padding: 12px max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar-actions { display: flex; gap: 6px; align-items: center; }
.content { max-width: 760px; margin: 0 auto; padding: 24px 16px 64px; }
section + section { margin-top: 40px; }
.section-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 14px; }
.section-head .muted { margin-top: 2px; }
.notice {
  background: var(--warn-soft); color: var(--warn-text); border-radius: var(--radius);
  padding: 12px 16px; margin-bottom: 24px; font-size: 0.93rem;
}

/* menu */
.menu { position: relative; }
.menu summary { list-style: none; }
.menu summary::-webkit-details-marker { display: none; }
.menu-panel {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 230px; z-index: 10;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 10px 30px rgb(0 0 0 / 0.15); padding: 6px;
}
.menu-email { padding: 8px 10px; font-size: 0.85rem; color: var(--muted); border-bottom: 1px solid var(--border); margin-bottom: 4px; overflow-wrap: anywhere; }
.menu-item {
  display: block; width: 100%; text-align: left; font: inherit; font-size: 0.93rem; color: var(--text);
  background: none; border: 0; border-radius: 8px; padding: 9px 10px; cursor: pointer;
}
.menu-item:hover { background: var(--surface-2); }
.menu-item.danger { color: var(--danger); }

/* ---------- codes */
.codes, .activity { list-style: none; margin: 0; padding: 0; }
.codes {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
}
.code-row { display: flex; align-items: center; gap: 14px; padding: 14px 16px; }
.code-row + .code-row { border-top: 1px solid var(--border); }
.code-row.expired { opacity: 0.6; }
.code-avatar {
  width: 40px; height: 40px; border-radius: 12px; flex: none; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent); font-weight: 700;
}
.code-row.expired .code-avatar { background: var(--surface-2); color: var(--muted); }
.code-main { flex: 1; min-width: 0; }
.code-title { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.code-title strong { font-weight: 600; overflow-wrap: anywhere; }
.code-hint { font: 0.85rem ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--muted); letter-spacing: 0.05em; }
.code-meta { font-size: 0.85rem; color: var(--muted); margin-top: 2px; }
.pill { font-size: 0.75rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.pill.expired { background: var(--danger-soft); color: var(--danger); }
.pill.soon { background: var(--warn-soft); color: var(--warn-text); }
.icon-btn {
  flex: none; width: 36px; height: 36px; border-radius: 10px; border: 0; background: transparent;
  color: var(--muted); cursor: pointer; display: grid; place-items: center;
}
.icon-btn:hover { background: var(--danger-soft); color: var(--danger); }
.icon-btn svg { width: 18px; height: 18px; }
.empty {
  border: 1px dashed var(--border); border-radius: var(--radius); padding: 28px 16px;
  text-align: center; color: var(--muted); font-size: 0.93rem;
}

/* ---------- activity */
.activity li { display: flex; gap: 12px; padding: 9px 0; font-size: 0.92rem; }
.activity li + li { border-top: 1px solid var(--border); }
.activity .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--border); margin-top: 8px; flex: none; }
.activity .dot.door { background: var(--accent); }
.activity .dot.denied, .activity .dot.error { background: var(--danger); }
.activity .dot.login { background: #5b8def; }
.activity .when { color: var(--muted); font-size: 0.82rem; white-space: nowrap; margin-left: auto; padding-left: 8px; }
.activity .what { overflow-wrap: anywhere; }

/* ---------- dialogs */
dialog {
  width: min(440px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); overflow: auto;
  border: 1px solid var(--border); border-radius: 18px; padding: 24px; color: var(--text);
  background: var(--surface); box-shadow: 0 20px 60px rgb(0 0 0 / 0.25);
}
dialog::backdrop { background: rgb(0 0 0 / 0.4); backdrop-filter: blur(2px); }
dialog h2 { margin-bottom: 4px; }
dialog h2 + .muted { margin-top: 4px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 22px; }
.big-code {
  margin: 20px 0 14px; padding: 18px; border-radius: 12px; background: var(--surface-2);
  font: 600 2rem ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.12em;
  text-align: center; overflow-wrap: anywhere; user-select: all;
}
.share-text { font-size: 0.9rem; color: var(--muted); white-space: pre-line; }

/* ---------- toast */
.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-size: 0.92rem;
  font-weight: 500; opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 50;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 520px) {
  .section-head { align-items: flex-start; }
  .code-row { padding: 12px 14px; gap: 12px; }
  .dialog-actions .btn { flex: 1; }
}

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