*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:         #f7f7f8;
  --surface:    #ffffff;
  --border:     #e5e7eb;
  --text:       #111827;
  --muted:      #6b7280;
  --accent:     #2563eb;
  --accent-bg:  #eff6ff;
  --danger:     #dc2626;
  --pin-bg:     #fffbeb;
  --pin-badge-bg:   #fef3c7;
  --pin-badge-text: #92400e;
  --radius:     10px;
  --radius-sm:  6px;
}

:root[data-theme="dark"] {
  --bg:         #16181d;
  --surface:    #1e2128;
  --border:     #2f333d;
  --text:       #e5e7eb;
  --muted:      #9199a6;
  --accent:     #3b82f6;
  --accent-bg:  #1e293b;
  --danger:     #f87171;
  --pin-bg:     #1a2332;
  --pin-badge-bg:   var(--accent-bg);
  --pin-badge-text: var(--accent);
}

:root[data-theme="dark"] input:focus,
:root[data-theme="dark"] textarea:focus { background: var(--surface); }

html { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  height: 100dvh;
  overflow: hidden;
}

/* ── Auth screens ───────────────────────────────────────────── */

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

.auth-wrap[hidden] { display: none; }

.auth-box {
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-title {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.5px;
  text-align: center;
}

.auth-hint {
  font-size: 14px;
  color: var(--muted);
  text-align: center;
}

/* ── App shell ──────────────────────────────────────────────── */

#app[hidden] { display: none; }

#app {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

/* ── Screens ────────────────────────────────────────────────── */

.screen {
  display: none;
  flex-direction: column;
  height: 100dvh;
  flex-shrink: 0;
}

.screen.active { display: flex; width: 100%; }

@media (min-width: 640px) {
  #topics-screen {
    display: flex !important;
    width: 256px;
    border-right: 1px solid var(--border);
    position: relative;
  }
  #feed-screen {
    display: flex !important;
    flex: 1;
  }
  #back-btn { display: none !important; }
}

/* ── Header ─────────────────────────────────────────────────── */

.hdr {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 8px 0 16px;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.hdr-title {
  flex: 1;
  font-size: 17px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.btn-icon[hidden] { display: none; }

.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: none;
  background: transparent;
  cursor: pointer;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 18px;
  flex-shrink: 0;
}

.btn-icon:hover { background: var(--border); }
.btn-icon.danger { color: var(--danger); }

/* ── Scroll area ────────────────────────────────────────────── */

.scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* ── Topic rows ─────────────────────────────────────────────── */

.topic-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  height: 52px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  user-select: none;
}

.topic-row:hover,
.topic-row.active { background: var(--accent-bg); }

.topic-row-title {
  flex: 1;
  font-size: 15px;
  font-weight: 500;
}

.topic-row-arrow { color: var(--muted); font-size: 14px; }

/* ── FAB ────────────────────────────────────────────────────── */

#fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(37,99,235,.4);
}

@media (min-width: 640px) {
  #fab { position: absolute; bottom: 20px; right: 16px; }
}

/* ── Thought cards ──────────────────────────────────────────── */

.thought-card {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.thought-card.pinned { background: var(--pin-bg); }

.thought-text {
  font-size: 15px;
  white-space: pre-wrap;
  word-break: break-word;
}

.thought-source { margin-top: 5px; font-size: 13px; }
.thought-source a { color: var(--accent); text-decoration: none; }
.thought-source a:hover { text-decoration: underline; }
.thought-source .src-label { color: var(--muted); }

.thought-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}

.t-time { font-size: 12px; color: var(--muted); }

.pin-badge {
  font-size: 11px;
  font-weight: 600;
  background: var(--pin-badge-bg);
  color: var(--pin-badge-text);
  padding: 1px 7px;
  border-radius: 999px;
}

.topic-badge {
  font-size: 11px;
  font-weight: 500;
  background: var(--accent-bg);
  color: var(--accent);
  padding: 1px 7px;
  border-radius: 999px;
}

.t-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 8px;
}

.btn-sm {
  padding: 5px 10px;
  border: 1px solid var(--border);
  background: transparent;
  border-radius: var(--radius-sm);
  font-size: 13px;
  cursor: pointer;
  color: var(--muted);
  min-height: 32px;
  font-family: inherit;
}

.btn-sm:hover { background: var(--bg); color: var(--text); }
.btn-sm.danger:hover { color: var(--danger); border-color: var(--danger); }

.vote-wrap {
  display: flex;
  align-items: center;
  gap: 2px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  height: 32px;
}

.btn-vote {
  background: transparent;
  border: none;
  padding: 0 8px;
  font-size: 12px;
  cursor: pointer;
  color: var(--muted);
  height: 100%;
  line-height: 1;
}

.btn-vote:hover { background: var(--bg); color: var(--text); }

.vote-score {
  font-size: 13px;
  font-weight: 600;
  min-width: 24px;
  text-align: center;
  color: var(--text);
}

.sort-select {
  font-size: 13px;
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--muted);
  padding: 0 6px;
  height: 32px;
  cursor: pointer;
  outline: none;
}

/* ── Replies ────────────────────────────────────────────────── */

.replies-toggle {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin-top: 10px;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}

.replies-toggle:hover { color: var(--accent); }
.replies-toggle .caret { font-size: 10px; }

.replies-body {
  margin-top: 8px;
  padding-left: 14px;
  border-left: 3px solid var(--border);
}

.reply-card {
  padding: 10px 0;
  border-bottom: 1px solid #f3f4f6;
}

.reply-card:last-child { border-bottom: none; }

.reply-text {
  font-size: 14px;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ── Inline forms ───────────────────────────────────────────── */

.inline-form {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.inline-form-btns { display: flex; gap: 8px; }
.inline-form-btns > * { flex: 1; }

/* ── Add-thought bar ────────────────────────────────────────── */

#add-bar {
  flex-shrink: 0;
  padding: 10px 14px 14px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.add-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  margin-top: 6px;
}

.add-source { flex: 1; }

/* ── Form elements ──────────────────────────────────────────── */

input, textarea {
  font-size: 16px;
  font-family: inherit;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  width: 100%;
  display: block;
}

textarea { resize: none; line-height: 1.5; overflow: hidden; }

input:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  background: #fff;
}

.btn-primary {
  padding: 10px 18px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  min-height: 44px;
  font-family: inherit;
  white-space: nowrap;
}

.btn-primary:hover { background: #1d4ed8; }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }

.btn-secondary {
  padding: 10px 18px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  min-height: 44px;
  font-family: inherit;
}

.btn-secondary:hover { background: var(--bg); }

.pin-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
  flex-shrink: 0;
  padding: 0 2px;
}

.pin-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
}

/* ── Modal ──────────────────────────────────────────────────── */

#modal-bg {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.25);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 200;
}

#modal-bg[hidden] { display: none; }

#modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,.15);
}

.modal-title { font-size: 17px; font-weight: 600; }
.modal-row { display: flex; gap: 10px; }
.modal-row > * { flex: 1; }

/* ── Misc ───────────────────────────────────────────────────── */

.empty {
  padding: 56px 24px;
  text-align: center;
  color: var(--muted);
  font-size: 15px;
}

.err-text { font-size: 13px; color: var(--danger); text-align: center; }

#feed-hint {
  display: none;
  flex: 1;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 15px;
}

@media (min-width: 640px) {
  #feed-hint.visible { display: flex; }
}
