:root {
  --bg: #0f172a;
  --bg-soft: #1e293b;
  --card: #1e293b;
  --card-2: #273449;
  --line: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --primary: #38bdf8;
  --primary-ink: #0b2233;
  --good: #34d399;
  --bad: #fb7185;
  --warn: #fbbf24;
  --shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  line-height: 1.45;
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--muted);
  font-weight: 400;
}

.error {
  color: var(--bad);
  font-size: 0.9rem;
}

/* ---------- Gate / picker ---------- */
.gate {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.gate-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 32px 24px;
  width: 100%;
  max-width: 360px;
  text-align: center;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.gate-emoji {
  font-size: 44px;
}

.gate-card h1 {
  margin: 0;
  font-size: 1.5rem;
}

.gate-card p {
  margin: 0;
}

.who-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 8px;
}

.who-btn {
  padding: 18px 8px;
  font-size: 1.1rem;
  font-weight: 600;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--card-2);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.08s ease, border-color 0.15s ease;
}

.who-btn:active {
  transform: scale(0.97);
}

.who-btn:hover {
  border-color: var(--primary);
}

/* ---------- Layout ---------- */
#app {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px calc(40px + env(safe-area-inset-bottom));
}

@media (min-width: 900px) {
  #app {
    max-width: 860px;
  }
  .chart-wrap {
    height: 300px;
  }
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  background: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 4px;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--line);
}

.brand {
  font-weight: 700;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.who-pill {
  background: var(--card-2);
  border: 1px solid var(--line);
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.card h2 {
  margin: 0 0 14px;
  font-size: 1.15rem;
}

/* ---------- Tabs ---------- */
.tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.tab {
  flex: 1;
  background: var(--card);
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 12px;
  padding: 11px 10px;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
}

.tab.active {
  background: var(--primary);
  color: var(--primary-ink);
  border-color: var(--primary);
}

/* ---------- Say / voice logging ---------- */
.say-card label {
  display: block;
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 8px;
}

.say-thread {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 10px;
}

.say-turn {
  font-size: 0.9rem;
  padding: 9px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
}

.say-turn.me {
  background: var(--card-2);
  color: var(--muted);
}

.say-turn.you {
  background: var(--card-2);
  color: var(--muted);
}

.say-turn.ask {
  background: rgba(56, 189, 248, 0.12);
  border-color: rgba(56, 189, 248, 0.35);
  color: var(--text);
  font-weight: 600;
}

.say-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-top: 10px;
}

.say-status {
  margin: 12px 0 0;
  padding: 10px 12px;
  border-radius: 12px;
  font-size: 0.9rem;
  font-weight: 600;
  text-align: center;
  background: var(--card-2);
  border: 1px solid var(--line);
  color: var(--muted);
}

.say-status.ok {
  background: rgba(52, 211, 153, 0.12);
  border-color: rgba(52, 211, 153, 0.35);
  color: var(--good);
}

.say-status.bad {
  background: rgba(251, 113, 133, 0.12);
  border-color: rgba(251, 113, 133, 0.35);
  color: var(--bad);
}

/* ---------- Today summary ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 10px;
}

.stat {
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
}

.stat-label {
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 600;
}

.stat-val {
  font-size: 1.4rem;
  font-weight: 800;
  line-height: 1.2;
}

.stat-sub {
  font-size: 0.75rem;
  color: var(--muted);
}

.since-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.since {
  background: rgba(56, 189, 248, 0.1);
  border: 1px solid rgba(56, 189, 248, 0.3);
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 600;
  padding: 5px 11px;
  border-radius: 999px;
}

.since.stale {
  background: rgba(251, 191, 36, 0.12);
  border-color: rgba(251, 191, 36, 0.4);
}

/* ---------- Quick log ---------- */
.quick-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 10px;
}

.quick-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px 8px;
  color: var(--text);
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.08s ease, border-color 0.15s ease;
}

.quick-btn:hover {
  border-color: var(--primary);
}

.quick-btn:active {
  transform: scale(0.96);
}

.quick-emoji {
  font-size: 26px;
  line-height: 1;
}

/* ---------- Filters ---------- */
.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 12px;
}

.history-count {
  font-size: 0.8rem;
  margin: -4px 0 12px;
}

/* ---------- Filter chips ---------- */
.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}

.chip {
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}

.chip.active {
  background: var(--primary);
  color: var(--primary-ink);
  border-color: var(--primary);
}

/* Four tabs have to fit across a phone, so tighten them below tablet width. */
@media (max-width: 560px) {
  .tabs {
    gap: 6px;
  }
  .tab {
    font-size: 0.8rem;
    padding: 10px 4px;
  }
}

/* On a phone the type chips would wrap to five rows — swipe them instead. */
@media (max-width: 560px) {
  .chip-row {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
  }
  .chip-row::-webkit-scrollbar {
    display: none;
  }
  .chip {
    flex: 0 0 auto;
  }
  /* Example questions read better wrapped than swiped. */
  .ask-examples {
    flex-wrap: wrap;
    overflow: visible;
  }
  .ask-examples .chip {
    flex: 0 1 auto;
    white-space: normal;
    text-align: left;
  }
  .seg {
    flex: 1 1 auto;
    justify-content: center;
  }
  .seg button {
    flex: 1 1 auto;
    padding: 8px 10px;
  }
}

/* ---------- History ---------- */
.history {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.day-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin: 10px 0 2px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--line);
}

.day-head:first-child {
  margin-top: 0;
}

.day-name {
  font-weight: 700;
  font-size: 0.95rem;
}

.day-sum {
  font-size: 0.78rem;
  color: var(--muted);
}

.entry {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  width: 100%;
  text-align: left;
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 11px 13px;
  color: var(--text);
  font: inherit;
  cursor: pointer;
  transition: border-color 0.15s ease;
}

.entry:hover {
  border-color: var(--primary);
}

.entry-emoji {
  font-size: 22px;
  line-height: 1.2;
}

.entry-body {
  flex: 1;
  min-width: 0;
}

.entry-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.entry-title {
  font-weight: 700;
}

.entry-time {
  font-size: 0.82rem;
  color: var(--muted);
  white-space: nowrap;
}

.entry-detail {
  font-size: 0.88rem;
  color: var(--muted);
}

.entry-notes {
  margin: 4px 0 0;
  font-size: 0.88rem;
  white-space: pre-wrap;
}

.entry-meta {
  margin-top: 5px;
  font-size: 0.74rem;
  color: var(--muted);
}

.entry-meta .lag {
  color: var(--warn);
}

.name-badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--c);
  background: color-mix(in srgb, var(--c) 18%, transparent);
  border: 1px solid color-mix(in srgb, var(--c) 45%, transparent);
  padding: 0 7px;
  border-radius: 999px;
  margin-left: 6px;
  vertical-align: 1px;
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 18px;
}

.foot {
  text-align: center;
  font-size: 0.82rem;
  margin-top: 8px;
}

/* ---------- Charts ---------- */
.chart-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.chart-head h2 {
  margin: 0;
}

.seg {
  display: inline-flex;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}

.seg button {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 7px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.seg button.active {
  background: var(--primary);
  color: var(--primary-ink);
}

.chart-wrap {
  position: relative;
  height: 240px;
  margin-bottom: 18px;
}

/* ---------- Sheet (add/edit) ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(2, 6, 23, 0.7);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  overflow-y: auto;
}

@media (min-width: 700px) {
  .sheet-backdrop {
    align-items: center;
    padding: 24px;
  }
  .sheet {
    border-radius: 20px !important;
  }
}

.sheet {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px 22px 0 0;
  width: 100%;
  max-width: 520px;
  padding: 20px 18px calc(20px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
  animation: slide-up 0.18s ease;
}

@keyframes slide-up {
  from {
    transform: translateY(14px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.sheet-head h2 {
  margin: 0;
  font-size: 1.2rem;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 4px 8px;
}

.icon-btn:hover {
  color: var(--text);
}

.sheet-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-top: 6px;
}

/* ---------- Form ---------- */
.field {
  margin-bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 0.9rem;
  font-weight: 600;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

input,
textarea,
select {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--text);
  padding: 13px 14px;
  font-size: 1rem;
  font-family: inherit;
  width: 100%;
}

input:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.18);
}

/* iOS Safari: stop native date/time inputs from overflowing the rounded field. */
input[type="date"],
input[type="datetime-local"] {
  -webkit-appearance: none;
  appearance: none;
  min-width: 0;
  max-width: 100%;
  min-height: 1.4em;
  text-align: left;
}

input[type="datetime-local"]::-webkit-date-and-time-value {
  text-align: left;
  margin: 0;
}

textarea {
  resize: vertical;
}

/* Segmented choice buttons inside the sheet */
.opts {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.opt {
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  flex: 1 1 auto;
}

.opt.active {
  background: var(--primary);
  color: var(--primary-ink);
  border-color: var(--primary);
}

.now-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.now-row input {
  flex: 1;
}

.btn-primary {
  background: var(--primary);
  color: var(--primary-ink);
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  flex: 1;
  transition: transform 0.08s ease, filter 0.15s ease;
}

.btn-primary:active {
  transform: scale(0.98);
}

.btn-primary:hover {
  filter: brightness(1.05);
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 9px 14px;
  font-size: 0.86rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.btn-ghost:hover {
  color: var(--text);
  border-color: var(--primary);
}

.btn-danger {
  background: transparent;
  color: var(--bad);
  border: 1px solid rgba(251, 113, 133, 0.4);
  border-radius: 12px;
  padding: 14px 18px;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
}

.btn-danger:hover {
  background: rgba(251, 113, 133, 0.12);
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--card-2);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 12px 18px;
  border-radius: 12px;
  box-shadow: var(--shadow);
  z-index: 50;
  font-weight: 600;
  max-width: 90vw;
  text-align: center;
}
