:root {
  --bg: #f6f5f1; --surface: #ffffff; --surface-2: #f1efe9; --text: #1d1b18; --muted: #6b665d; --border: #e6e1d8;
  --accent: #2f5bea; --accent-2: #1a3aa8; --accent-soft: #eef2ff; --on-accent: #ffffff;
  --good: #1f7a4d; --good-soft: #e5f4ec; --warn: #a15c00; --warn-soft: #fff3df; --bad: #b3261e; --bad-soft: #fde9e7;
  --shadow: 0 1px 2px rgba(29, 27, 24, .05), 0 6px 20px rgba(29, 27, 24, .05);
  --radius: 16px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121110; --surface: #1c1b19; --surface-2: #262420; --text: #f2efe9; --muted: #a9a297; --border: #33302b;
    --accent: #7c9cff; --accent-2: #a8bcff; --accent-soft: #1d2440; --on-accent: #0e1220;
    --good: #5cc99a; --good-soft: #15291f; --warn: #f0b35b; --warn-soft: #2d2212; --bad: #ff8a80; --bad-soft: #351816;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121110; --surface: #1c1b19; --surface-2: #262420; --text: #f2efe9; --muted: #a9a297; --border: #33302b;
  --accent: #7c9cff; --accent-2: #a8bcff; --accent-soft: #1d2440; --on-accent: #0e1220;
  --good: #5cc99a; --good-soft: #15291f; --warn: #f0b35b; --warn-soft: #2d2212; --bad: #ff8a80; --bad-soft: #351816;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans", sans-serif; }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.2; letter-spacing: -0.01em; }
h1 { font-size: 24px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
[hidden] { display: none !important; }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: 100vh; }
.side { position: sticky; top: 0; height: 100vh; padding: 20px 14px; border-right: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; gap: 4px; }
.logo { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 800; font-size: 17px; }
.logo .mark { width: 34px; height: 34px; border-radius: 10px; background: var(--accent); color: var(--on-accent); display: grid; place-items: center; font-weight: 800; }
.logo small { display: block; font-weight: 500; color: var(--muted); font-size: 12px; }
.nav { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 10px; border: 0; background: none; cursor: pointer; text-align: left; color: var(--muted); font-weight: 600; width: 100%; }
.nav svg { width: 20px; height: 20px; flex: none; }
.nav:hover { background: var(--surface-2); color: var(--text); }
.nav[aria-current="page"] { background: var(--accent-soft); color: var(--accent); }
.nav .badge { margin-left: auto; }
.side .spacer { flex: 1; }
main { padding: 28px 32px 60px; max-width: 1180px; width: 100%; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; }

@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .side { position: fixed; inset: auto 0 0 0; height: auto; flex-direction: row; padding: 6px 6px calc(6px + env(safe-area-inset-bottom)); border-right: 0; border-top: 1px solid var(--border); z-index: 20; justify-content: space-around; gap: 0; }
  .logo, .side .spacer, .side .logout { display: none; }
  .nav { flex-direction: column; gap: 2px; font-size: 11px; padding: 6px 4px; justify-content: center; align-items: center; text-align: center; }
  .nav .badge { position: absolute; top: 0; left: calc(50% + 4px); margin: 0; padding: 0 6px; font-size: 10px; line-height: 16px; }
  .chat .head { gap: 8px; }
  .chat .head .clip-name { flex-basis: calc(100% - 100px); }
  .nav { position: relative; }
  main { padding: 18px 16px 100px; }
  h1 { font-size: 21px; }
}

/* ---------- Pieces ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); min-width: 0; }
.card + .card, .grid + .card, .card + .grid, .grid + .grid, .banner + .card { margin-top: 16px; }
.grid > .card + .card { margin-top: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.grid { display: grid; gap: 16px; }
.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 1000px) { .cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .cols-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 640px) { .cols-2, .cols-3 { grid-template-columns: 1fr; } .cols-4 { gap: 10px; } }

.stat .label { color: var(--muted); font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 6px; }
.stat .value { font-size: 28px; font-weight: 800; margin-top: 6px; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.stat .sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
@media (max-width: 640px) { .stat { padding: 14px; } .stat .value { font-size: 23px; } }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 40px; padding: 8px 16px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-weight: 650; white-space: nowrap; text-decoration: none; color: var(--text); }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--bad); }
.btn.small { min-height: 32px; padding: 4px 12px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: default; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 700; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; display: inline-block; }

/* Status banner */
.banner { display: flex; align-items: center; gap: 14px; padding: 16px 18px; border-radius: var(--radius); margin-bottom: 18px; border: 1px solid var(--border); background: var(--surface); flex-wrap: wrap; }
.banner .pulse { width: 12px; height: 12px; border-radius: 50%; background: var(--good); box-shadow: 0 0 0 0 color-mix(in srgb, var(--good) 50%, transparent); animation: pulse 2s infinite; flex: none; }
.banner.off .pulse { background: var(--warn); animation: none; }
.banner .grow { flex: 1; min-width: 200px; }
@keyframes pulse { 70% { box-shadow: 0 0 0 10px transparent; } 100% { box-shadow: 0 0 0 0 transparent; } }
@media (prefers-reduced-motion: reduce) { .banner .pulse { animation: none; } }

/* Toggle */
.switch { position: relative; display: inline-flex; align-items: center; gap: 10px; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track { width: 44px; height: 26px; border-radius: 999px; background: var(--border); position: relative; transition: background .2s; flex: none; }
.switch .track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

.setting { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 0; border-bottom: 1px solid var(--border); }
.setting:last-child { border-bottom: 0; }
.setting p { margin: 2px 0 0; }

input[type="text"], input[type="password"], input[type="number"], input[type="time"], textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); min-height: 42px; }
textarea { resize: vertical; min-height: 110px; line-height: 1.45; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
label.field { display: block; font-weight: 600; font-size: 13px; margin: 12px 0 6px; }

/* Lists */
.list { display: flex; flex-direction: column; }
.item { display: flex; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--border); align-items: flex-start; }
.item:last-child { border-bottom: 0; }
.item .icon { width: 32px; height: 32px; border-radius: 10px; display: grid; place-items: center; background: var(--surface-2); flex: none; font-size: 15px; }
.item .body { flex: 1; min-width: 0; }
.item .when { color: var(--muted); font-size: 12px; white-space: nowrap; }
.clip { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { padding: 28px 10px; text-align: center; color: var(--muted); }

/* Posts */
.posts { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(250px, 100%), 1fr)); gap: 16px; }
.post { padding: 0; overflow: hidden; display: flex; flex-direction: column; }
.post .thumb { aspect-ratio: 4 / 5; background: var(--surface-2); position: relative; display: grid; place-items: center; overflow: hidden; }
.post .thumb img, .post .thumb video { width: 100%; height: 100%; object-fit: cover; display: block; }
.post .thumb .tag { position: absolute; top: 10px; left: 10px; }
.post .thumb .fmt { position: absolute; top: 10px; right: 10px; background: rgba(0,0,0,.6); color: #fff; }
.post .meta { padding: 14px; display: flex; flex-direction: column; gap: 8px; flex: 1; }
.post .meta .row:last-child { margin-top: auto; padding-top: 4px; }
.post .caption { font-size: 13px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.post .nums { display: flex; gap: 12px; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.filters { display: flex; gap: 6px; margin-bottom: 16px; overflow-x: auto; padding-bottom: 2px; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 14px; cursor: pointer; font-weight: 600; font-size: 13px; white-space: nowrap; }
.chip[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }

/* Inbox */
.inbox { display: grid; grid-template-columns: 320px 1fr; gap: 16px; align-items: start; }
.inbox .threads { padding: 6px; max-height: 72vh; overflow: auto; }
.thread { display: flex; gap: 10px; padding: 10px; border-radius: 12px; cursor: pointer; border: 0; background: none; width: 100%; text-align: left; align-items: center; }
.thread:hover { background: var(--surface-2); }
.thread[aria-current="true"] { background: var(--accent-soft); }
.avatar { width: 38px; height: 38px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 800; flex: none; text-transform: uppercase; }
.chat { display: flex; flex-direction: column; min-height: 60vh; padding: 0; }
.chat .head { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.chat .msgs { flex: 1; padding: 18px; display: flex; flex-direction: column; gap: 8px; overflow: auto; max-height: 56vh; }
.bubble { max-width: 78%; padding: 9px 13px; border-radius: 16px; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 14px; }
.bubble.them { background: var(--surface-2); align-self: flex-start; border-bottom-left-radius: 4px; }
.bubble.bot, .bubble.you { background: var(--accent); color: var(--on-accent); align-self: flex-end; border-bottom-right-radius: 4px; }
.bubble.you { background: var(--text); color: var(--bg); }
.bubble small { display: block; opacity: .7; font-size: 11px; margin-top: 3px; }
.chat form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border); }
.chat form input { flex: 1; }
#back { display: none; }
@media (max-width: 860px) { #back { display: inline-flex; } .inbox { grid-template-columns: 1fr; } .inbox.open .threads { display: none; } .inbox:not(.open) .chat { display: none; } }

/* Charts */
.bars { display: flex; align-items: flex-end; gap: 4px; height: 120px; padding-top: 10px; }
.bars .bar { flex: 1; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 2px; opacity: .9; position: relative; }
.bars .bar:hover { opacity: 1; }
.bars .bar.zero { background: var(--border); }
.axis { display: flex; justify-content: space-between; color: var(--muted); font-size: 11px; margin-top: 6px; }

.meter { height: 10px; border-radius: 999px; background: var(--surface-2); overflow: hidden; margin-top: 8px; }
.meter span { display: block; height: 100%; background: var(--accent); border-radius: 999px; }
.meter.hot span { background: var(--warn); }

/* Setup */
.steps { counter-reset: s; display: flex; flex-direction: column; gap: 12px; }
.step { display: flex; gap: 14px; }
.step .num { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; background: var(--surface-2); font-weight: 800; flex: none; font-size: 14px; }
.step.done .num { background: var(--good-soft); color: var(--good); }
.step .body { flex: 1; min-width: 0; }
.step ol, .step ul { margin: 6px 0 0; padding-left: 18px; color: var(--muted); }
.step li { margin: 3px 0; }
code { background: var(--surface-2); padding: 1px 6px; border-radius: 6px; font-size: 13px; overflow-wrap: anywhere; }

/* Modal + toast */
dialog { border: 0; border-radius: 20px; padding: 0; width: min(640px, calc(100vw - 32px)); background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog .inner { padding: 22px; max-height: 85vh; overflow: auto; }
dialog .preview { display: grid; grid-template-columns: 200px 1fr; gap: 18px; }
dialog .preview img, dialog .preview video { width: 100%; border-radius: 12px; display: block; }
@media (max-width: 600px) { dialog .preview { grid-template-columns: 1fr; } dialog .preview img, dialog .preview video { max-width: 220px; } }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 12px; font-weight: 600; z-index: 50; max-width: calc(100vw - 32px); }
@media (max-width: 860px) { .toast { bottom: 90px; } }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: radial-gradient(circle at 20% 10%, var(--accent-soft), transparent 50%), var(--bg); }
.login .card { width: min(380px, 100%); padding: 28px; }
.login .mark { width: 52px; height: 52px; border-radius: 15px; background: var(--accent); color: var(--on-accent); display: grid; place-items: center; font-weight: 800; font-size: 24px; margin-bottom: 16px; }
.error { color: var(--bad); font-weight: 600; min-height: 1.5em; margin: 10px 0 0; font-size: 14px; }
