:root {
  --bg: #0d0f12;
  --bg-2: #14171c;
  --bg-3: #1b1f26;
  --line: #262b33;
  --text: #e8e6e0;
  --muted: #8b9099;
  --gold: #c8a24a;
  --gold-soft: #d8b86a;
  --in: #6fae84;
  --out: #5a8fc7;
  --warn: #cf6a5a;
  --serif: Georgia, 'Times New Roman', serif;
  --sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --radius: 14px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; }

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 820px;
  margin: 0 auto;
}

/* ---------------- Header ---------------- */
#hdr {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 9px; }
.lotus { color: var(--gold); font-size: 22px; line-height: 1; }
.brand-name { font-family: var(--serif); font-size: 18px; letter-spacing: .2px; }
.hdr-actions { display: flex; gap: 8px; }
.hdr-btn {
  background: var(--bg-3); border: 1px solid var(--line); color: var(--muted);
  border-radius: 8px; padding: 6px 12px; font-size: 13px;
}
.hdr-btn:hover { color: var(--text); border-color: var(--gold); }

/* ---------------- Status strip ---------------- */
#statusbar {
  display: block; width: calc(100% - 24px); margin: 10px 12px 0; text-align: left; cursor: pointer;
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 14px; color: var(--text); flex-shrink: 0;
}
#statusbar:hover { border-color: #34404e; }
.sb-row { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.sb-cal b { font-family: var(--serif); font-size: 20px; color: var(--text); }
.sb-sub { color: var(--muted); font-size: 12px; }
.sb-net { font-size: 13px; }
.sb-track { height: 6px; background: var(--bg-3); border-radius: 4px; overflow: hidden; border: 1px solid var(--line); }
.sb-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--in), var(--gold)); transition: width .3s; }
.sb-macros { display: flex; gap: 12px; margin-top: 8px; font-size: 11px; color: var(--muted); flex-wrap: nowrap; overflow: hidden; }
.sb-macros span { white-space: nowrap; }
.sb-macros span b { color: var(--text); font-weight: 600; }

/* ---------------- Chat ---------------- */
.thread {
  flex: 1; min-height: 0; overflow-y: auto;
  padding: 18px 16px 8px;
  display: flex; flex-direction: column; gap: 14px;
}
.msg { max-width: 88%; font-size: 15px; line-height: 1.55; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user {
  align-self: flex-end; background: var(--gold); color: #1a1407;
  padding: 10px 14px; border-radius: 16px; border-bottom-right-radius: 5px; white-space: pre-wrap;
}
.msg.assistant { align-self: flex-start; max-width: 100%; }
.msg.assistant.typing { color: var(--muted); font-style: italic; }
.msg-img { max-width: 220px; border-radius: 12px; display: block; margin-bottom: 8px; }
.event-pill {
  align-self: center; font-size: 11px; color: var(--gold);
  background: rgba(200,162,74,.1); border: 1px solid rgba(200,162,74,.3);
  border-radius: 20px; padding: 4px 12px;
}

/* Markdown inside assistant messages */
.msg.assistant p { margin: 0 0 10px; }
.msg.assistant p:last-child { margin-bottom: 0; }
.msg.assistant h3, .msg.assistant h4, .msg.assistant h5 { font-family: var(--serif); font-weight: normal; margin: 4px 0 8px; font-size: 16px; }
.msg.assistant strong { color: #fff; font-weight: 600; }
.msg.assistant code { background: var(--bg-3); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.msg.assistant ul, .msg.assistant ol { margin: 0 0 10px; padding-left: 20px; }
.msg.assistant li { margin: 2px 0; }
.msg.assistant a { color: var(--gold-soft); }
.msg.assistant .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 4px 0 12px; border: 1px solid var(--line); border-radius: 10px; }
.msg.assistant table {
  min-width: 100%; border-collapse: collapse;
  font-size: 13px; background: var(--bg-2);
}
.msg.assistant th, .msg.assistant td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--line); }
.msg.assistant th { background: var(--bg-3); color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: .5px; }
.msg.assistant td:not(:first-child), .msg.assistant th:not(:first-child) { text-align: right; white-space: nowrap; }
.msg.assistant tr:last-child td { border-bottom: none; }
.msg.assistant tbody tr:last-child { font-weight: 600; background: rgba(200,162,74,.06); }

/* ---------------- Composer ---------------- */
#composer {
  display: flex; align-items: flex-end; gap: 10px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: var(--bg);
  flex-shrink: 0;
}
.icon-btn {
  background: var(--bg-3); border: 1px solid var(--line); color: var(--text);
  width: 40px; height: 40px; border-radius: 12px; font-size: 20px; flex-shrink: 0;
}
.icon-btn:hover { border-color: var(--gold); color: var(--gold); }
#input {
  flex: 1; resize: none; max-height: 140px;
  background: var(--bg-3); border: 1px solid var(--line); color: var(--text);
  border-radius: 12px; padding: 10px 14px; font-size: 15px; line-height: 1.4;
}
#input:focus { outline: none; border-color: var(--gold); }
.send-btn {
  background: var(--gold); color: #1a1407; border: none;
  width: 40px; height: 40px; border-radius: 12px; font-size: 20px; flex-shrink: 0;
}
.send-btn:disabled { opacity: .4; cursor: default; }
.img-preview { position: relative; flex-shrink: 0; }
.img-preview img { width: 40px; height: 40px; object-fit: cover; border-radius: 10px; }
.img-preview button { position: absolute; top: -8px; right: -8px; background: var(--warn); color: #fff; border: none; border-radius: 50%; width: 22px; height: 22px; font-size: 12px; line-height: 1; }

/* ---------------- Modal ---------------- */
.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.6); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; padding: 18px; z-index: 50;
}
.card {
  background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 22px; width: 100%; max-width: 440px; max-height: 88dvh; overflow-y: auto; position: relative;
}
.modal-close { position: absolute; top: 8px; right: 8px; width: 40px; height: 40px; display: flex; align-items: center; justify-content: center; background: transparent; border: none; color: var(--muted); font-size: 18px; border-radius: 8px; }
.modal-close:hover { color: var(--text); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.detail-h { font-family: var(--serif); font-size: 20px; margin: 0 0 4px; }
.detail-sub { color: var(--muted); font-size: 13px; margin-bottom: 16px; }
.detail-section { margin-bottom: 16px; }
.detail-section h3 { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin: 0 0 8px; }

/* Tables in modals */
.dtable { width: 100%; border-collapse: collapse; font-size: 13px; }
.dtable th, .dtable td { padding: 7px 8px; border-bottom: 1px solid var(--line); text-align: left; }
.dtable th { color: var(--muted); font-size: 10px; text-transform: uppercase; letter-spacing: .5px; }
.dtable td:not(:first-child), .dtable th:not(:first-child) { text-align: right; white-space: nowrap; }
.dtable tbody tr.total td { font-weight: 700; border-top: 2px solid var(--line); border-bottom: none; }

/* History cards */
.day-card { background: var(--bg-3); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; cursor: pointer; }
.day-card:hover { border-color: #34404e; }
.dc-date { font-size: 12px; color: var(--muted); margin-bottom: 5px; }
.dc-row { display: flex; justify-content: space-between; align-items: baseline; }
.dc-net { font-family: var(--serif); font-size: 17px; }
.dc-net.deficit { color: var(--in); }
.dc-net.surplus { color: var(--warn); }
.dc-io { color: var(--muted); font-size: 11px; }

/* ---------------- Pyramid (progress modal) ---------------- */
.pyramid-pct { font-family: var(--serif); font-size: 30px; color: var(--gold); text-align: center; }
.pyramid-cap { color: var(--muted); font-size: 12px; text-align: center; margin-bottom: 14px; }
.pyramid { display: flex; flex-direction: column; align-items: center; gap: 3px; margin: 6px 0 16px; max-width: 100%; overflow-x: auto; }
.brick-row { display: flex; gap: 3px; }
.brick { width: 14px; height: 8px; border-radius: 2px; background: var(--gold); box-shadow: inset 0 -2px 0 rgba(0,0,0,.25); }
.brick.cleared { background: #2a2f37; }
.explain { background: var(--bg-3); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; font-size: 13px; line-height: 1.6; color: var(--muted); }
.explain b { color: var(--text); }
.future-note { color: var(--muted); font-size: 11px; text-align: center; margin-top: 12px; font-style: italic; }

@media (max-width: 760px) {
  .brick { width: 12px; }
}
