:root {
  --bg: #f6f7fb;
  --panel: #ffffff;
  --panel-2: #f1f3f9;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #4f46e5;
  --accent-soft: #eef2ff;
  --ok: #16a34a;
  --ok-soft: #dcfce7;
  --bad: #dc2626;
  --bad-soft: #fee2e2;
  --urgent: #ef4444;
  --high: #f97316;
  --normal: #eab308;
  --low: #94a3b8;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 16px rgba(15, 23, 42, .04);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1020;
    --panel: #121a2e;
    --panel-2: #18223a;
    --text: #e5e9f2;
    --muted: #8b97ad;
    --border: #243049;
    --accent: #818cf8;
    --accent-soft: #1e2546;
    --ok: #4ade80;
    --ok-soft: #12301f;
    --bad: #f87171;
    --bad-soft: #3a1717;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--bad); min-height: 1.2em; margin: 0; }
h1 { font-size: 22px; margin: 0 0 4px; letter-spacing: -.01em; }
h2 { font-size: 16px; margin: 0 0 12px; }
a { color: var(--accent); }
code { font: 13px ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--panel-2); padding: 2px 6px; border-radius: 6px; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%); background: var(--panel); border: 1px solid var(--border); border-radius: 20px;
  padding: 32px 24px; display: grid; gap: 12px; text-align: center; box-shadow: var(--shadow);
}
.logo {
  width: 48px; height: 48px; border-radius: 14px; margin: 0 auto 4px; display: grid; place-items: center;
  background: var(--accent); color: white; font-weight: 700; font-size: 22px;
}
.logo.small { width: 28px; height: 28px; font-size: 14px; border-radius: 8px; margin: 0; }

/* ---------- layout ---------- */
/* minmax(0, 1fr): long unbroken text (emails, invoice ids) must never widen the page */
.app { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh; }
.view, .card, .panel, .list, .stack { min-width: 0; }
.who, .subject, .summary, .muted, .original pre, .reply, .code-row span { overflow-wrap: anywhere; }
.mobile-bar { display: none; }
.nav {
  position: sticky; top: 0; height: 100vh; padding: 16px 12px; display: flex; flex-direction: column; gap: 2px;
  background: var(--panel); border-right: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; padding: 4px 10px 16px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 10px;
  color: var(--text); text-decoration: none; font-weight: 500;
}
.nav a:hover { background: var(--panel-2); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav .ico { width: 20px; text-align: center; }
.badge {
  margin-left: auto; min-width: 20px; padding: 0 6px; border-radius: 10px; font-size: 12px; line-height: 20px;
  text-align: center; background: var(--accent); color: white;
}
.badge:empty { display: none; }
.nav-logout { margin-top: auto; background: none; border: 0; color: var(--muted); text-align: left; padding: 9px 10px; cursor: pointer; font: inherit; }
.view { padding: 28px 32px 96px; max-width: 920px; width: 100%; min-width: 0; }
.view-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }

@media (max-width: 760px) {
  html, body { overflow-x: hidden; }
  .app { grid-template-columns: minmax(0, 1fr); }
  .mobile-bar {
    display: flex; align-items: center; gap: 10px; position: sticky; top: 0; z-index: 9;
    padding: calc(10px + env(safe-area-inset-top)) 16px 10px; font-weight: 650;
    background: color-mix(in srgb, var(--panel) 92%, transparent); backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--border);
  }
  .mobile-bar button { margin-left: auto; background: none; border: 0; color: var(--muted); font: inherit; font-size: 14px; cursor: pointer; }
  h1 { font-size: 20px; }
  .view-head { margin-bottom: 14px; }
  .seg { display: flex; flex-direction: column; width: 100%; }
  .seg button { text-align: left; }
  .code-row { flex-direction: column; align-items: flex-start; gap: 4px; }
  .code-row code { word-break: break-all; }
  .ask { flex-direction: column; }
  .ask .btn { width: 100%; }
  .actions .btn { flex: 1 1 auto; }
  .stats { gap: 8px; }
  .stat { padding: 10px; }
  .stat b { font-size: 22px; }
  .event .time { width: 78px; font-size: 13px; }
  .chips { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin-right: -16px; padding-right: 16px; }
  .chip { flex: none; }
  .nav {
    position: fixed; top: auto; bottom: 0; left: 0; right: 0; height: auto; z-index: 10;
    flex-direction: row; justify-content: space-around; padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    border-right: 0; border-top: 1px solid var(--border);
  }
  .brand, .nav-logout { display: none; }
  .nav a { flex-direction: column; gap: 0; padding: 6px 2px; font-size: 11px; position: relative; flex: 1; text-align: center; white-space: nowrap; min-width: 0; }
  .nav a .ico { font-size: 18px; }
  .badge { position: absolute; top: 0; right: calc(50% - 22px); margin: 0; font-size: 10px; min-width: 16px; line-height: 16px; padding: 0 4px; }
  .view { padding: 20px 16px 110px; }
}

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 14px;
  border-radius: 10px; border: 1px solid var(--border); background: var(--panel); color: var(--text);
  font: inherit; font-weight: 550; cursor: pointer; white-space: nowrap; text-decoration: none;
}
.btn:hover { background: var(--panel-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: white; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.danger.armed { background: var(--bad); border-color: var(--bad); color: white; }
.btn.small { padding: 5px 10px; font-size: 13px; }
input[type=text], input[type=password], input[type=tel], input[type=datetime-local], input[type=number], input[type=time], select, textarea {
  width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); font: inherit;
}
textarea { resize: vertical; min-height: 84px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
label.field { display: grid; gap: 6px; font-weight: 550; font-size: 14px; }
label.field .hint { font-weight: 400; color: var(--muted); font-size: 13px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }

.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 14px; }
.chip {
  padding: 5px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--panel);
  color: var(--muted); font: inherit; font-size: 13px; font-weight: 550; cursor: pointer;
}
.chip.active { background: var(--text); border-color: var(--text); color: var(--bg); }

.switch { position: relative; display: inline-block; width: 42px; height: 24px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--border); border-radius: 24px; transition: .15s; cursor: pointer; }
.switch span::before { content: ""; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px; background: white; border-radius: 50%; transition: .15s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::before { transform: translateX(18px); }

/* ---------- panels & cards ---------- */
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin-bottom: 16px; }
.panel > h2:first-child { margin-top: 0; }
.stack { display: grid; gap: 14px; }
.list { display: grid; gap: 10px; }

.ask { display: flex; gap: 8px; margin-bottom: 18px; }
.ask input { padding: 12px 14px; border-radius: 12px; }
.reply { background: var(--accent-soft); border-radius: 12px; padding: 10px 14px; margin: -8px 0 18px; white-space: pre-wrap; }

.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; gap: 10px; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; background: var(--low); }
.dot.urgent { background: var(--urgent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--urgent) 20%, transparent); }
.dot.high { background: var(--high); }
.dot.normal { background: var(--normal); }
.who { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta { margin-left: auto; color: var(--muted); font-size: 13px; white-space: nowrap; }
.tag { font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 999px; background: var(--panel-2); color: var(--muted); }
.tag.sent { background: var(--ok-soft); color: var(--ok); }
.subject { font-weight: 600; margin-top: 6px; }
.scam { margin-top: 8px; padding: 8px 12px; border-radius: 10px; background: var(--bad-soft); color: var(--bad); font-size: 14px; }
.summary { margin: 4px 0 0; }
.original { margin-top: 10px; }
.original summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.original pre { white-space: pre-wrap; font: inherit; font-size: 14px; background: var(--panel-2); padding: 10px 12px; border-radius: 10px; max-height: 280px; overflow: auto; margin: 8px 0 0; }
.draft { margin-top: 12px; display: grid; gap: 8px; }
.draft-label { font-size: 12px; font-weight: 650; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty .big { font-size: 36px; }

.day { margin-bottom: 18px; }
.day h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 0 0 8px; }
.event { display: flex; gap: 14px; padding: 10px 14px; background: var(--panel); border: 1px solid var(--border); border-left: 3px solid var(--accent); border-radius: 10px; margin-bottom: 6px; }
.event .time { width: 96px; flex: none; color: var(--muted); font-variant-numeric: tabular-nums; }

.check { display: flex; gap: 12px; align-items: flex-start; padding: 12px 0; border-bottom: 1px solid var(--border); }
.check:last-child { border-bottom: 0; }
.check .state { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; flex: none; font-size: 14px; }
.check .state.ok { background: var(--ok-soft); color: var(--ok); }
.check .state.bad { background: var(--bad-soft); color: var(--bad); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 16px; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.stat b { font-size: 26px; display: block; }

.codes { display: grid; gap: 8px; }
.code-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 12px; background: var(--panel-2); border-radius: 10px; }
.steps { padding-left: 20px; margin: 8px 0 0; }
.steps li { margin-bottom: 6px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: var(--panel); color: var(--text); padding: 8px 14px; font: inherit; cursor: pointer; }
.seg button.active { background: var(--accent); color: white; }
.rings { display: flex; gap: 6px; }
.rings button { width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--border); background: var(--panel); color: var(--text); font: inherit; font-weight: 650; cursor: pointer; }
.rings button.active { background: var(--accent); border-color: var(--accent); color: white; }

.toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 50; width: min(420px, calc(100% - 32px)); }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 12px; box-shadow: 0 8px 24px rgba(0,0,0,.2); animation: pop .15s ease-out; }
.toast.bad { background: var(--bad); color: white; }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }
@media (max-width: 760px) { .toasts { bottom: 90px; } }
.spinner { width: 16px; height: 16px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
