:root {
  --bg: #f6f3ff;
  --panel: #ffffff;
  --text: #241b3d;
  --muted: #7a7096;
  --accent: #6d4aff;
  --accent-text: #ffffff;
  --bubble: #e6defe;
  --bubble-text: #241b3d;
  --line: #e3dcf7;
  --danger: #c2345b;
  --shadow: 0 2px 10px rgba(80, 50, 160, .12);
}
:root[data-theme="dark"] {
  --bg: #15111f;
  --panel: #201a31;
  --text: #efeaff;
  --muted: #a69cc4;
  --accent: #9b82ff;
  --accent-text: #15111f;
  --bubble: #3a2f63;
  --bubble-text: #efeaff;
  --line: #34294f;
  --danger: #ff7a9c;
  --shadow: 0 2px 10px rgba(0, 0, 0, .4);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15111f;
    --panel: #201a31;
    --text: #efeaff;
    --muted: #a69cc4;
    --accent: #9b82ff;
    --accent-text: #15111f;
    --bubble: #3a2f63;
    --bubble-text: #efeaff;
    --line: #34294f;
    --danger: #ff7a9c;
    --shadow: 0 2px 10px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

#app { height: 100%; height: 100dvh; display: flex; flex-direction: column; max-width: 720px; margin: 0 auto; }
.admin #app { max-width: 960px; }

/* header */
.top {
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  background: var(--accent); color: var(--accent-text);
  box-shadow: var(--shadow); flex: none;
}
.top h1 { flex: 1; margin: 0; font-size: 1.15rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .sub { font-size: .8rem; opacity: .85; }
.icon-btn {
  background: transparent; border: 0; color: inherit; width: 40px; height: 40px;
  border-radius: 50%; font-size: 1.25rem; line-height: 1; flex: none;
}
.icon-btn:hover, .icon-btn:focus-visible { background: rgba(255, 255, 255, .2); outline: none; }

.content { flex: 1; overflow-y: auto; padding: 14px 14px 24px; }

/* stats strip */
.stats { display: flex; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.stat {
  flex: 1 1 90px; background: var(--panel); border-radius: 14px; padding: 10px 12px;
  box-shadow: var(--shadow); text-align: center;
}
.stat b { display: block; font-size: 1.35rem; color: var(--accent); }
.stat span { font-size: .78rem; color: var(--muted); }

/* lists */
.card {
  display: block; width: 100%; text-align: left; background: var(--panel); border: 0;
  border-radius: 14px; padding: 14px; margin-bottom: 10px; box-shadow: var(--shadow);
}
.card:hover, .card:focus-visible { outline: 2px solid var(--accent); }
.card .t { font-weight: 600; }
.card .m { color: var(--muted); font-size: .85rem; margin-top: 2px; }
.empty { text-align: center; color: var(--muted); margin: 40px 10px; }

.btn {
  background: var(--accent); color: var(--accent-text); border: 0; border-radius: 12px;
  padding: 11px 16px; font-weight: 600;
}
.btn.ghost { background: transparent; color: var(--accent); border: 1.5px solid var(--accent); }
.btn.danger { background: var(--danger); color: #fff; }
.btn:disabled { opacity: .5; }
.row { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.row .btn { flex: 1; }

/* chat */
.chat { flex: 1; overflow-y: auto; padding: 14px 12px; display: flex; flex-direction: column; gap: 6px; }
.bubble {
  align-self: flex-end; max-width: 88%; background: var(--bubble); color: var(--bubble-text);
  border-radius: 16px 16px 4px 16px; padding: 8px 12px; white-space: pre-wrap; overflow-wrap: anywhere;
  box-shadow: var(--shadow); border: 0; text-align: left; cursor: pointer;
}
.bubble:focus-visible { outline: 2px solid var(--accent); }
.bubble .meta { display: block; font-size: .7rem; color: var(--muted); text-align: right; margin-top: 2px; }
.day { align-self: center; font-size: .75rem; color: var(--muted); background: var(--panel); border-radius: 10px; padding: 2px 10px; margin: 8px 0 2px; }

.compose {
  display: flex; align-items: flex-end; gap: 8px; flex: none; background: var(--panel);
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line);
}
.compose textarea {
  flex: 1; resize: none; max-height: 40vh; min-height: 44px; border-radius: 22px; border: 1.5px solid var(--line);
  background: var(--bg); padding: 10px 14px; line-height: 1.35;
}
.compose textarea:focus { outline: none; border-color: var(--accent); }
.send {
  flex: none; width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--accent);
  color: var(--accent-text); font-size: 1.2rem;
}
.send:disabled { opacity: .4; }
.compose.editing { border-top: 3px solid var(--accent); }
.edit-note { font-size: .78rem; color: var(--accent); padding: 4px 14px 0; background: var(--panel); flex: none; }

/* sheets and dialogs */
.sheet-bg { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: flex; align-items: flex-end; justify-content: center; z-index: 10; }
.sheet { background: var(--panel); width: 100%; max-width: 520px; border-radius: 20px 20px 0 0; padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); }
.sheet h2 { margin: 0 0 10px; font-size: 1.05rem; }
.sheet input[type=text], .login input {
  width: 100%; border-radius: 12px; border: 1.5px solid var(--line); background: var(--bg); padding: 11px 12px;
}
.sheet input:focus, .login input:focus { outline: none; border-color: var(--accent); }
.sheet .btn { width: 100%; margin-top: 8px; }

.login { margin: auto; width: min(360px, 90%); text-align: center; }
.login h1 { color: var(--accent); }
.login .btn { width: 100%; margin-top: 10px; }
.error { color: var(--danger); min-height: 1.4em; margin: 8px 0 0; font-size: .9rem; }

#toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 8px 16px; border-radius: 20px; font-size: .9rem; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 20;
}
#toast.show { opacity: 1; }

/* admin */
.admin .content { padding-bottom: 40px; }
.admin .hid { opacity: .6; border-left: 4px solid var(--danger); }
.tag { display: inline-block; font-size: .7rem; border-radius: 8px; padding: 1px 7px; background: var(--danger); color: #fff; margin-left: 6px; vertical-align: middle; }
.admin .bubble { align-self: stretch; max-width: 100%; cursor: default; }
.admin .hist { margin: 6px 0 0; padding: 6px 10px; border-left: 3px solid var(--line); color: var(--muted); font-size: .85rem; white-space: pre-wrap; }
.admin .small-btn { background: transparent; border: 1px solid var(--accent); color: var(--accent); border-radius: 8px; padding: 2px 8px; margin: 4px 6px 0 0; font-size: .8rem; }

@media (prefers-reduced-motion: reduce) { #toast { transition: none; } }

.grow { flex: 1; min-width: 0; }
.linkbtn { display: inline-flex; align-items: center; justify-content: center; text-align: center; text-decoration: none; color: inherit; }
.btn.ghost.linkbtn { color: var(--accent); }
.icon-btn.linkbtn { color: inherit; }
.btn.ghost.onbar { background: #fff; color: #6d4aff; border-color: #fff; padding: 6px 10px; font-size: .85rem; }
.linklike { background: none; border: 0; color: var(--accent); text-decoration: underline; padding: 0 4px; }
.edit-note { display: flex; justify-content: space-between; align-items: center; padding-right: 14px; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; }
.icon-btn svg { display: block; }
