:root {
  --bg: #0f1729;
  --bg2: #16203a;
  --surface: #1b274a;
  --line: #263257;
  --text: #e7ecf5;
  --muted: #8595b8;
  --accent: #4f8cff;
  --user: #2f6f5f;
  --bot: #1e2a4d;
  --err: #f08a7a;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
}

/* ---------- Login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login-card {
  width: 100%; max-width: 340px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 18px; padding: 30px 26px; text-align: center;
  display: flex; flex-direction: column; gap: 12px;
}
.login-logo {
  width: 52px; height: 52px; border-radius: 15px; margin: 0 auto 6px;
  background: var(--accent); color: #fff; font-weight: 700; font-size: 26px;
  display: grid; place-items: center;
}
.login-card h1 { font-size: 1.3rem; margin: 0; }
.muted { color: var(--muted); margin: 0; font-size: .92rem; }
.login-card input[type=password] {
  width: 100%; padding: 13px 15px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg2); color: var(--text); font-size: 1rem;
}
.login-card button {
  padding: 13px; border: none; border-radius: 12px; background: var(--accent);
  color: #fff; font-size: 1rem; font-weight: 600; cursor: pointer;
}
.err { color: var(--err); font-size: .9rem; }

/* ---------- App shell ---------- */
#app { display: flex; flex-direction: column; height: 100dvh; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top));
  background: var(--bg2); border-bottom: 1px solid var(--line); flex: 0 0 auto;
}
.topbar .title { font-weight: 600; }
.topbar-actions { display: flex; align-items: center; gap: 14px; }
.topbar .logout { color: var(--muted); text-decoration: none; font-size: 1.25rem; }
.topbar .gconnect {
  color: var(--accent); text-decoration: none; font-size: .82rem; font-weight: 600;
  border: 1px solid var(--accent); border-radius: 20px; padding: 4px 11px;
}

.chat-log {
  flex: 1 1 auto; overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 10px;
}
.bubble {
  max-width: 82%; padding: 9px 13px; border-radius: 15px;
  line-height: 1.45; white-space: pre-wrap; word-wrap: break-word;
  font-size: .97rem; animation: pop .15s ease;
}
.bubble.user { align-self: flex-end; background: var(--user); border-bottom-right-radius: 4px; }
.bubble.bot  { align-self: flex-start; background: var(--bot); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.bubble img.sent { max-width: 180px; border-radius: 10px; display: block; margin-bottom: 6px; }
.bubble.typing { color: var(--muted); font-style: italic; }
@keyframes pop { from { transform: translateY(4px); opacity: 0; } }

.composer {
  flex: 0 0 auto; display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--bg2); border-top: 1px solid var(--line);
}
.composer input[type=text] {
  flex: 1 1 auto; padding: 11px 15px; border-radius: 22px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text); font-size: 1rem;
}
.composer .attach { cursor: pointer; font-size: 1.35rem; padding: 4px 2px; }
.composer #send {
  border: none; background: var(--accent); color: #fff; width: 42px; height: 42px;
  border-radius: 50%; font-size: 1.1rem; cursor: pointer; flex: 0 0 auto;
}
.composer #send:disabled { opacity: .5; }
.preview { position: relative; }
.preview img { width: 40px; height: 40px; object-fit: cover; border-radius: 8px; display: block; }
.preview #preview-x {
  position: absolute; top: -6px; right: -6px; width: 18px; height: 18px; border-radius: 50%;
  border: none; background: #000; color: #fff; font-size: 12px; line-height: 1; cursor: pointer;
}

/* ---------- Dashboard ---------- */
.dash { max-width: 640px; margin: 0 auto; padding: 16px 16px 40px; padding-top: max(16px, env(safe-area-inset-top)); }
.dash-top { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 18px; }
.dash-top .hello { font-size: 1.5rem; font-weight: 700; }
.dash-top .today { color: var(--muted); font-size: .9rem; margin-top: 2px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 16px 18px; margin-bottom: 16px; }
.card h2 { font-size: .95rem; margin: 0 0 12px; color: var(--text); }
.upcoming .grp-label { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 12px 0 6px; }
.upcoming .grp-label:first-of-type { margin-top: 0; }
.up-row { display: flex; gap: 10px; align-items: baseline; padding: 4px 0; font-size: .95rem; }
.up-time { color: var(--accent); font-size: .8rem; white-space: nowrap; min-width: 92px; }
.up-row .dot { color: var(--accent); font-size: .6rem; }
.up-text { flex: 1; }

.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 4px; }
.tile {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  aspect-ratio: 1 / 1; border-radius: 18px; text-decoration: none; color: var(--text);
  background: var(--surface); border: 1px solid var(--line); font-size: .9rem; font-weight: 600;
  transition: transform .1s;
}
.tile:active { transform: scale(.96); }
.tile .ic { font-size: 1.9rem; }
.tile.diary { background: linear-gradient(160deg, #24304f, #1b2540); }
.tile.finance { background: linear-gradient(160deg, #1e3a34, #172a28); }
.tile.chat { background: linear-gradient(160deg, #2b2550, #1c1b3d); }

/* ---------- Sub-page header ---------- */
.topbar.sub { justify-content: space-between; }
.topbar .back { color: var(--text); text-decoration: none; font-size: 1.4rem; width: 24px; }

/* ---------- Diary (ruled notebook) ---------- */
.paper-wrap { max-width: 640px; margin: 0 auto; padding: 16px; }
.paper {
  background: #fbf7ec; color: #2b2a26; border-radius: 8px; padding: 18px 20px 24px 46px;
  position: relative; box-shadow: var(--shadow, 0 6px 20px rgba(0,0,0,.3));
  background-image: repeating-linear-gradient(#fbf7ec 0, #fbf7ec 27px, #cfe0ec 28px);
  background-size: 100% 28px; line-height: 28px;
  font-family: "Segoe Print", "Comic Sans MS", "Bradley Hand", cursive, system-ui;
}
.paper::before { content: ""; position: absolute; top: 0; bottom: 0; left: 34px; width: 2px; background: #e6a9a0; }
.entry { margin-bottom: 20px; }
.entry-date { font-weight: 700; color: #7a4; color: #3a7d5a; font-family: system-ui; font-size: .8rem; line-height: 28px; }
.paper-line { min-height: 28px; }
.entry-empty { color: #8a7; font-style: italic; }
.readonly-note { text-align: center; color: var(--muted); font-size: .8rem; margin-top: 14px; }

/* ---------- Finance ---------- */
.finance { max-width: 640px; margin: 0 auto; padding: 16px 16px 40px; }
.fin-total { text-align: center; }
.fin-label { color: var(--muted); font-size: .85rem; }
.fin-big { font-size: 2.1rem; font-weight: 700; margin-top: 4px; }
.bar-row { margin: 10px 0; }
.bar-head { display: flex; justify-content: space-between; font-size: .9rem; margin-bottom: 5px; }
.bar-track { background: var(--bg2); border-radius: 6px; height: 9px; overflow: hidden; }
.bar-fill { background: var(--accent); height: 100%; border-radius: 6px; }
.trend { display: flex; align-items: flex-end; gap: 8px; height: 110px; padding-top: 8px; }
.trend-col { flex: 1; display: flex; flex-direction: column; align-items: center; height: 100%; justify-content: flex-end; }
.trend-bar { width: 60%; background: var(--accent); border-radius: 5px 5px 0 0; min-height: 3px; }
.trend-label { font-size: .72rem; color: var(--muted); margin-top: 6px; }
.tx-row { display: flex; justify-content: space-between; padding: 9px 0; border-bottom: 1px solid var(--line); }
.tx-row:last-child { border-bottom: none; }
.tx-cat { font-weight: 600; font-size: .95rem; }
.tx-note { color: var(--muted); font-size: .82rem; }
.tx-right { text-align: right; }
.tx-amt { font-weight: 600; font-variant-numeric: tabular-nums; }
.tx-date { color: var(--muted); font-size: .78rem; }
