:root {
  --bg: #10161A;
  --surface: #182124;
  --surface-2: #1E2A2D;
  --text: #ECEAE2;
  --text-muted: #94A19B;
  --amber: #E8A55C;
  --amber-dim: #4A3B26;
  --teal: #6FA89D;
  --teal-dim: #1D2D2A;
  --border: #2A3538;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
[hidden] { display: none !important; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 720px;
  margin: 0 auto;
}

.chat-view {
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* ---------- auth screen ---------- */

.auth-view {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px 26px;
}

.auth-card .brand { justify-content: center; margin-bottom: 8px; }

.auth-sub {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
  margin: 0 0 20px;
  line-height: 1.5;
}

.auth-tabs {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
  margin-bottom: 18px;
}
.auth-tab {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.85rem;
  padding: 8px 0;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.auth-tab.active {
  background: var(--amber-dim);
  color: var(--amber);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.auth-form input {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-family: inherit;
  /* iOS Safari auto-zooms into any input with a computed font-size under
     16px on focus, and can leave the page stuck zoomed in afterwards — so
     this must never go below 1rem/16px. */
  font-size: 1rem;
  padding: 11px 13px;
}
.auth-form input:focus {
  outline: none;
  border-color: var(--teal);
}
.auth-form button {
  margin-top: 6px;
  background: var(--amber);
  color: #10161A;
  border: none;
  border-radius: 10px;
  padding: 11px 0;
  font-weight: 600;
  font-size: 0.92rem;
  cursor: pointer;
}
.auth-form button:disabled { opacity: 0.6; cursor: not-allowed; }

.checkbox-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.5;
  cursor: pointer;
}
.checkbox-row input[type="checkbox"] {
  margin-top: 2px;
  flex-shrink: 0;
  accent-color: var(--amber);
}
.checkbox-row a { color: var(--teal); }

.legal-links {
  text-align: center;
  margin-top: 16px;
  font-size: 0.76rem;
  color: var(--text-muted);
}
.legal-links a { color: var(--text-muted); text-decoration: none; }
.legal-links a:hover { color: var(--text); }
.legal-links span { margin: 0 6px; }

.auth-error {
  margin-top: 14px;
  font-size: 0.82rem;
  color: #C97A6B;
  text-align: center;
  line-height: 1.5;
}

.verify-links {
  display: flex;
  justify-content: space-between;
  margin-top: 14px;
}
.verify-links .text-btn { padding: 4px 6px; font-size: 0.8rem; }

/* ---------- settings modal ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 30;
}

.modal-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 22px 24px;
  animation: rise 0.25s ease both;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  font-size: 1.1rem;
  margin-bottom: 20px;
}
.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}
.modal-close:hover { color: var(--text); }

.modal-section { margin-bottom: 22px; }
.modal-section:last-child { margin-bottom: 0; }

.danger-zone {
  border-top: 1px solid var(--border);
  padding-top: 22px;
}
.danger-zone .modal-hint { margin: 6px 0 14px; line-height: 1.5; }
button.danger-btn {
  width: 100%;
  margin-top: 0;
  background: transparent;
  border: 1px solid #C97A6B;
  color: #C97A6B;
  border-radius: 10px;
  padding: 11px 0;
  font-family: inherit;
  font-weight: 600;
  font-size: 0.92rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
button.danger-btn:hover { background: #C97A6B; color: var(--bg); }
button.danger-btn:disabled { opacity: 0.6; cursor: not-allowed; }
#cancelDeleteAccountBtn { margin-top: 8px; }

.settings-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  max-width: 420px;
  width: 100%;
  margin: 0 auto;
}

.permission-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.permission-row:last-child { margin-bottom: 0; }

.referral-link-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
}
.referral-link-row input {
  flex: 1;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 0.85rem;
}
.referral-link-row .ghost-btn { flex-shrink: 0; }

/* ---------- settings folders ---------- */

.folder-list {
  display: flex;
  flex-direction: column;
}

.folder-row {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
  margin-bottom: 10px;
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
  text-align: left;
  cursor: pointer;
  transition: border-color 0.15s;
}
.folder-row:hover { border-color: var(--amber); }
.folder-row:last-child { margin-bottom: 0; }

.folder-row-hint {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--text-muted);
}
.folder-row-hint .chevron {
  color: var(--amber);
  font-size: 0.9rem;
}

.settings-folder .folder-back {
  background: none;
  border: none;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.85rem;
  padding: 0;
  margin-bottom: 20px;
  cursor: pointer;
}
.settings-folder .folder-back:hover { color: var(--text); }

/* ---------- calendar ---------- */

.calendar-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  max-width: 420px;
  width: 100%;
  margin: 0 auto;
}

.calendar-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}
.calendar-nav .text-btn {
  font-size: 1.2rem;
  padding: 4px 12px;
}
.calendar-month-label {
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  font-size: 1.05rem;
  color: var(--text);
  text-transform: capitalize;
}

.calendar-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  margin-bottom: 6px;
}
.calendar-weekdays span {
  text-align: center;
  font-size: 0.68rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  margin-bottom: 8px;
}

.calendar-day {
  position: relative;
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}
.calendar-day:disabled {
  visibility: hidden;
  cursor: default;
}
.calendar-day:not(:disabled):hover { border-color: var(--border); }
.calendar-day.has-note::after {
  content: "";
  position: absolute;
  bottom: 6px;
  width: 5px; height: 5px;
  border-radius: 50%;
  background: var(--amber);
}
.calendar-day.selected {
  background: var(--amber-dim);
  border-color: var(--amber);
  color: var(--amber);
}

.calendar-hint {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-bottom: 18px;
  line-height: 1.5;
}

.calendar-detail {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 20px;
  animation: rise 0.2s ease both;
}
.calendar-detail-note {
  font-size: 0.95rem;
  color: var(--text);
  margin-bottom: 10px;
  line-height: 1.5;
}
.calendar-detail-positive {
  font-size: 0.85rem;
  color: var(--teal);
  line-height: 1.5;
}
.calendar-detail-positive::before {
  content: "🌿 ";
}

.calendar-loading {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
  padding: 20px 0;
}

/* ---------- admin dashboard ---------- */

.admin-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  max-width: 520px;
  width: 100%;
  margin: 0 auto;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}

.stat-card-wide {
  grid-column: 1 / -1;
}
.stat-card-wide .stat-value { color: var(--teal); }

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
}

.stat-label {
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.stat-value {
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  font-size: 1.6rem;
  color: var(--amber);
}

.admin-hint {
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.5;
}

.error-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 340px;
  overflow-y: auto;
}

.error-row {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid #C97A6B;
  border-radius: 8px;
  padding: 10px 12px;
}

.error-row-meta {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.7rem;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.error-row-category { color: #C97A6B; }

.error-row-message {
  font-size: 0.82rem;
  color: var(--text);
  line-height: 1.4;
  word-break: break-word;
}

.error-row-path {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.modal-label {
  font-size: 0.68rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--amber);
  margin-bottom: 8px;
}
.modal-value { font-size: 0.95rem; margin-bottom: 4px; word-break: break-word; }
.modal-hint { font-size: 0.78rem; color: var(--text-muted); }

.install-steps {
  margin: 0;
  padding-left: 20px;
  font-size: 0.9rem;
  line-height: 1.7;
  color: var(--text);
}
.install-steps li { margin-bottom: 4px; }
.kbd {
  display: inline-block;
  padding: 1px 6px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface-2);
  font-size: 0.85em;
}

.auth-success { color: var(--teal) !important; }

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  /* max() keeps the 16px fallback on regular browsers/PWA while pushing
     content below the Dynamic Island on notched devices in the native app —
     without it, taps on the menu button land in the island's touch-exclusion
     zone and never reach the page. */
  padding: max(16px, env(safe-area-inset-top)) 20px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.brand {
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  font-size: 1.55rem;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
.brand .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--amber);
  box-shadow: 0 0 0 4px var(--amber-dim);
  flex-shrink: 0;
}
.brand .tag {
  font-family: -apple-system, sans-serif;
  font-size: 0.7rem;
  color: var(--text-muted);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px 10px;
}

.menu-wrap {
  position: relative;
}
.menu-toggle {
  width: 38px;
  height: 38px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 9px;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
.menu-toggle:hover { border-color: var(--amber); color: var(--amber); }

.menu-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 230px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.4);
  z-index: 20;
  display: flex;
  flex-direction: column;
}
.menu-divider {
  height: 1px;
  background: var(--border);
  margin: 6px 4px;
}
.menu-item {
  background: transparent;
  border: none;
  color: var(--text);
  text-align: left;
  font-family: inherit;
  font-size: 0.9rem;
  padding: 10px 10px;
  border-radius: 8px;
  cursor: pointer;
  width: 100%;
}
.menu-item:hover { background: var(--surface-2); }
.menu-item-danger { color: #C97A6B; }
.menu-item-link {
  display: block;
  text-decoration: none;
  box-sizing: border-box;
}
.menu-item-danger:hover { background: rgba(201, 122, 107, 0.12); }

.ghost-btn, .text-btn {
  font-family: inherit;
  font-size: 0.82rem;
  cursor: pointer;
  border-radius: 8px;
  white-space: nowrap;
  flex-shrink: 0;
  transition: background 0.15s, border-color 0.15s;
}
.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 14px;
}
.ghost-btn:hover { border-color: var(--amber); color: var(--amber); }
.ghost-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.text-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  padding: 4px 6px;
}
.text-btn:hover { color: var(--text); }

.chat {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.msg {
  max-width: 78%;
  padding: 12px 15px;
  border-radius: 14px;
  line-height: 1.5;
  font-size: 0.95rem;
  white-space: pre-wrap;
  word-wrap: break-word;
  animation: rise 0.35s ease both;
}
@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg.assistant {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg.user {
  align-self: flex-end;
  background: var(--teal-dim);
  border: 1px solid var(--border);
  border-bottom-right-radius: 4px;
}
.msg.error {
  align-self: center;
  background: transparent;
  border: 1px dashed #A24E3F;
  color: #C97A6B;
  font-size: 0.85rem;
  max-width: 90%;
}

.crisis-card {
  align-self: center;
  max-width: 92%;
  width: 100%;
  background: var(--teal-dim);
  border: 1px solid var(--teal);
  border-radius: 14px;
  padding: 14px 16px;
  animation: rise 0.35s ease both;
}
.crisis-card-title {
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 10px;
  color: var(--text);
}
.crisis-card-body {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.crisis-card-body a {
  display: block;
  color: var(--text);
  text-decoration: none;
  font-size: 0.85rem;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  transition: border-color 0.15s;
}
.crisis-card-body a:hover { border-color: var(--teal); }
.crisis-card-body a strong { color: var(--amber); }

.proactive-tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.68rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--amber);
  margin-bottom: 6px;
}
.proactive-tag .glow {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--amber);
  box-shadow: 0 0 0 3px var(--amber-dim);
}

.typing {
  align-self: flex-start;
  display: flex;
  gap: 4px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  border-bottom-left-radius: 4px;
}
.typing span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.9rem;
  max-width: 40ch;
  line-height: 1.6;
}

.composer {
  /* Same reasoning as .topbar's safe-area padding, but for the home-indicator
     gesture zone at the bottom — keeps the send button clear of it. */
  padding: 14px 20px max(18px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.composer-field {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 6px 6px 6px 8px;
  transition: border-color 0.15s;
}
.composer-field:focus-within {
  border-color: var(--teal);
}
.composer-field textarea {
  flex: 1;
  resize: none;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: inherit;
  /* Must stay >= 16px/1rem — iOS Safari auto-zooms into focused inputs
     below that size and can leave the page stuck zoomed in afterwards. */
  font-size: 1rem;
  padding: 10px 6px;
  min-height: 26px;
  max-height: 160px;
}
.composer-field textarea:focus {
  outline: none;
}

button.attach-btn {
  flex-shrink: 0;
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: 50%;
  color: var(--text-muted);
  font-size: 1.15rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
button.attach-btn:hover { background: var(--surface-2); color: var(--amber); }
button.attach-btn:disabled { opacity: 0.5; cursor: not-allowed; }

button.send-btn {
  flex-shrink: 0;
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: var(--amber);
  border: none;
  border-radius: 50%;
  color: #10161A;
  cursor: pointer;
  transition: opacity 0.15s;
}
button.send-btn svg {
  width: 17px;
  height: 17px;
  transform: translateX(-1px);
}
button.send-btn:disabled { opacity: 0.5; cursor: not-allowed; }
button.attach-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.image-preview-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 20px 0;
  flex-shrink: 0;
}
.image-preview-thumb {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--border);
}
.image-preview-remove {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.75rem;
  cursor: pointer;
}
.image-preview-remove:hover { color: var(--text); border-color: var(--amber); }

.msg-image {
  display: block;
  max-width: 100%;
  max-height: 320px;
  border-radius: 10px;
  object-fit: cover;
}
.msg-image + div { margin-top: 8px; }

@media (max-width: 480px) {
  .msg { max-width: 88%; }
}

/* ---------- legal pages ---------- */

.legal-page {
  max-width: 720px;
  margin: 0 auto;
  padding: 32px 20px 64px;
}

.legal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 28px;
}
.legal-header .brand { text-decoration: none; color: var(--text); }

.legal-draft-tag {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--amber);
  border: 1px solid var(--amber-dim);
  background: var(--amber-dim);
  border-radius: 999px;
  padding: 4px 10px;
}

.legal-callout {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--amber);
  border-radius: 10px;
  padding: 16px 18px;
  margin-bottom: 36px;
  font-size: 0.88rem;
  line-height: 1.6;
  color: var(--text-muted);
}
.legal-callout strong { color: var(--text); }
.legal-callout a { color: var(--amber); }

.legal-content h1 {
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  font-size: 1.6rem;
  margin: 0 0 4px;
}
.legal-updated {
  color: var(--text-muted);
  font-size: 0.8rem;
  margin: 0 0 24px;
}
.legal-content h2 {
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  font-size: 1.05rem;
  margin: 28px 0 10px;
  color: var(--amber);
}
.legal-content p, .legal-content li {
  font-size: 0.92rem;
  line-height: 1.65;
  color: var(--text);
}
.legal-content ul { padding-left: 20px; margin: 0 0 14px; }
.legal-content li { margin-bottom: 6px; }
.legal-content a { color: var(--teal); }
.legal-content em { color: var(--text-muted); font-style: italic; }

.legal-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 48px 0;
}

.legal-footer {
  margin-top: 48px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}
.legal-footer a {
  color: var(--text-muted);
  font-size: 0.85rem;
  text-decoration: none;
}
.legal-footer a:hover { color: var(--text); }
