/* ── Font stacks ── */
:root {
  --ff: "Inter", system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --ff-serif: "Merriweather", "Times New Roman", Times, Georgia, serif;
  --ff-mono: "Fira Code", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

/* ── Fluid root font size ── */
:root { font-size: 100%; }
@media (min-width: 36rem) { :root { font-size: 106.25%; } }
@media (min-width: 48rem) { :root { font-size: 112.5%; } }
@media (min-width: 64rem) { :root { font-size: 118.75%; } }
@media (min-width: 80rem) { :root { font-size: 125%; } }

/* ── Theme variables ── */
:root {
  --bg: #fafaf8;
  --bg-card: #fff;
  --bg-header: rgba(242, 242, 240, .98);
  --bg-input: #fafafa;
  --bg-hover: #f4f4f5;
  --bg-preview: #fafaf8;
  --bg-code: #f4f4f5;
  --bg-blockquote: #f8f9ff;
  --text: #1a1916;
  --text-secondary: #71717a;
  --text-muted: #a1a1aa;
  --text-btn: #52525b;
  --border: #e4e4e7;
  --border-header: #e8e4dc;
  --border-hover: #a1a1aa;
  --border-pagination: #d4d4d8;
  --accent: #6366f1;
  --accent-bg: #eef2ff;
  --accent-border-light: #c7d2fe;
  --btn-primary-bg: #18181b;
  --btn-primary-hover: #3f3f46;
  --btn-primary-text: #fff;
  --error-bg: #fee2e2;
  --error-text: #b91c1c;
  --error-border: #fecaca;
  --code-text: #dc2626;
  --warning: #f97316;
  --danger: #ef4444;
  --scrollbar: #e4e4e7;
  --shadow-card-hover: 0 10px 24px rgba(0, 0, 0, .08), 0 2px 6px rgba(0, 0, 0, .04);
}

/* Auto dark mode (system preference) — blocked by explicit light choice */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121212;
    --bg-card: #1e1e1e;
    --bg-header: rgba(18, 18, 18, .98);
    --bg-input: #2a2a2a;
    --bg-hover: #2a2a2a;
    --bg-preview: #1a1a1a;
    --bg-code: #2a2a2a;
    --bg-blockquote: #1a1a2e;
    --text: #e4e4e7;
    --text-secondary: #a1a1aa;
    --text-muted: #8b8b93;
    --text-btn: #a1a1aa;
    --border: #333;
    --border-header: #333;
    --border-hover: #555;
    --border-pagination: #444;
    --accent: #818cf8;
    --accent-bg: #1e1b4b;
    --accent-border-light: #4338ca;
    --btn-primary-bg: #e4e4e7;
    --btn-primary-hover: #d4d4d8;
    --btn-primary-text: #121212;
    --error-bg: #450a0a;
    --error-text: #fca5a5;
    --error-border: #7f1d1d;
    --code-text: #f87171;
    --warning: #fb923c;
    --danger: #f87171;
    --scrollbar: #444;
    --shadow-card-hover: 0 10px 24px rgba(0, 0, 0, .5), 0 2px 6px rgba(0, 0, 0, .35);
  }
}

/* Manual dark mode override */
[data-theme="dark"] {
  --bg: #121212;
  --bg-card: #1e1e1e;
  --bg-header: rgba(18, 18, 18, .98);
  --bg-input: #2a2a2a;
  --bg-hover: #2a2a2a;
  --bg-preview: #1a1a1a;
  --bg-code: #2a2a2a;
  --bg-blockquote: #1a1a2e;
  --text: #e4e4e7;
  --text-secondary: #a1a1aa;
  --text-muted: #8b8b93;
  --text-btn: #a1a1aa;
  --border: #333;
  --border-header: #333;
  --border-hover: #555;
  --border-pagination: #444;
  --accent: #818cf8;
  --accent-bg: #1e1b4b;
  --accent-border-light: #4338ca;
  --btn-primary-bg: #e4e4e7;
  --btn-primary-hover: #d4d4d8;
  --btn-primary-text: #121212;
  --error-bg: #450a0a;
  --error-text: #fca5a5;
  --error-border: #7f1d1d;
  --code-text: #f87171;
  --warning: #fb923c;
  --danger: #f87171;
  --scrollbar: #444;
  --shadow-card-hover: 0 10px 24px rgba(0, 0, 0, .5), 0 2px 6px rgba(0, 0, 0, .35);
}

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

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
:focus:not(:focus-visible) { outline: none; }

body {
  margin: 0;
  font-family: var(--ff);
  font-optical-sizing: auto;
  -webkit-font-smoothing: antialiased;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  transition: background-color .2s, color .2s;
}

input, button, textarea, select { font: inherit; }

/* ── Layout ── */
.layout {
  display: flex;
  align-items: flex-start;
  gap: 2rem;
  max-width: 60rem;
  margin: 0 auto;
  padding: 2.5rem 1.5rem;
  min-height: 100vh;
}

/* ── Sidebar ── */
.sidebar {
  width: 20rem;
  flex-shrink: 0;
  position: sticky;
  top: 2.5rem;
  max-height: calc(100vh - 5rem);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  padding: 1.75rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .04), 0 4px 12px rgba(0, 0, 0, .04);
  transition: background-color .2s, border-color .2s;
}

.sidebar-top {
  display: flex;
  flex-direction: column;
}
.sidebar-top .site-description { margin-bottom: 2rem; }

.sidebar-bottom {
  margin-top: auto;
  padding-top: 1.5rem;
}

.site-title {
  font-weight: 700; font-size: 1.75rem;
  margin: 0; letter-spacing: -.01em;
  text-wrap: balance;
}
.site-title a {
  text-decoration: none; color: inherit;
}
.sidebar-nav { display: flex; gap: .5rem; flex-wrap: wrap; }
.site-description {
  color: var(--text-secondary);
  font-size: 0.95rem;
  line-height: 1.4;
  margin: 0;
}

/* ── Theme toggle ── */
.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  padding: 0;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  color: var(--text-secondary);
  transition: background .1s, color .1s;
}
.theme-toggle:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.theme-toggle svg {
  width: 16px;
  height: 16px;
}

/* Icon visibility: light mode shows moon, dark mode shows sun */
.icon-sun { display: none; }
.icon-moon { display: block; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .icon-sun { display: block; }
  :root:not([data-theme="light"]) .icon-moon { display: none; }
}
[data-theme="dark"] .icon-sun { display: block; }
[data-theme="dark"] .icon-moon { display: none; }
/* Explicit light override when system is dark */
[data-theme="light"] .icon-sun { display: none; }
[data-theme="light"] .icon-moon { display: block; }

/* ── Footer (inside sidebar) ── */
.footer-nav {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
}

.footer-link {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-secondary);
  text-decoration: none;
  border-bottom: 1px solid var(--border);
  transition: color 0.15s, border-color 0.15s;
}

.footer-link:hover {
  color: var(--text);
  border-bottom-color: var(--text-secondary);
}

.footer-link-auth {
  color: var(--text);
  border-bottom-color: var(--text-muted);
}

/* Sign-Out is a <button form="logout-form"> so we can POST to /logout/
   while keeping the button itself visually identical to the other footer
   links. The associated <form> lives at the end of <body>, hidden.
   Inherit family/color/line-height explicitly — do NOT use the `font`
   shorthand, which would also reset font-size and clobber .footer-link. */
button.footer-link {
  background: none;
  border: none;
  border-bottom: 1px solid var(--border);
  padding: 0;
  margin: 0;
  font-family: inherit;
  font-weight: inherit;
  line-height: inherit;
  color: inherit;
  cursor: pointer;
  vertical-align: baseline;
}
button.footer-link.footer-link-auth {
  border-bottom-color: var(--text-muted);
}

/* ── Buttons ── */
.btn {
  display: inline-flex; align-items: center; gap: .35rem;
  padding: .4rem .875rem;
  border-radius: 6px; border: 1px solid transparent;
  font-size: .875rem; font-weight: 500;
  text-decoration: none; cursor: pointer;
  transition: background .1s, opacity .1s; line-height: 1;
}
.btn-primary  { background: var(--btn-primary-bg); color: var(--btn-primary-text); }
.btn-primary:hover { background: var(--btn-primary-hover); }
.btn-ghost    { background: transparent; color: var(--text-secondary); border-color: var(--border); }
.btn-ghost:hover { background: var(--bg-hover); }
.btn-danger   { background: var(--error-bg); color: var(--error-text); border-color: var(--error-border); }
.btn-danger:hover { background: var(--error-border); }
.btn-sm       { padding: .25rem .6rem; font-size: .8rem; }

/* ── Layout ── */
.container { flex: 1; min-width: 0; max-width: 40rem; }

/* ── Feed ── */
.post-list { list-style: none; padding: 0; margin: 0; }

.post {
  display: flex; align-items: center; gap: 1.25rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.75rem;
  margin-bottom: 2rem;
  width: 100%;
  box-sizing: border-box;
  transition: box-shadow .2s ease, transform .2s ease, border-color .2s ease, background-color .2s;
}
.post:hover { box-shadow: var(--shadow-card-hover); transform: translateY(-3px); }
.post:last-child { margin-bottom: 0; }

.post-emoji-img {
  width: 76px; height: 76px;
  object-fit: contain; flex-shrink: 0;
}

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

.post-message {
  font-family: var(--ff-serif);
  font-size: 1.05rem; line-height: 1.6;
  margin: 0 0 .5rem; word-break: break-word; overflow-wrap: anywhere;
  text-wrap: pretty;
}

.post-meta {
  display: flex; align-items: center;
  justify-content: space-between; gap: .5rem;
}
.post-time { font-size: .8rem; color: var(--text-muted); cursor: pointer; }
.post-actions { display: flex; align-items: center; gap: .5rem; }
.post-actions form { margin: 0; }

/* ── Clickable timestamps ── */
.post-time-link {
  text-decoration: none;
  color: inherit;
  display: inline-block;
}
.post-time-link::before {
  content: '';
  display: inline-block;
  width: 0.95em;
  height: 0.95em;
  margin-right: 0.3em;
  vertical-align: -0.15em;
  opacity: 0.55;
  background-color: currentColor;
  -webkit-mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><circle cx='12' cy='12' r='10'/><polyline points='12 6 12 12 16 14'/></svg>") center / contain no-repeat;
          mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><circle cx='12' cy='12' r='10'/><polyline points='12 6 12 12 16 14'/></svg>") center / contain no-repeat;
  transition: opacity .15s;
}
.post-time-link:hover .post-time {
  color: var(--text-secondary);
  text-decoration: underline;
}
.post-time-link:hover::before {
  opacity: 0.85;
}

/* ── Post detail page ── */
.detail-nav { margin-bottom: 1.5rem; }
.back-link {
  display: inline-flex; align-items: center; gap: .35rem;
  font-size: .875rem; color: var(--text-secondary);
  text-decoration: none; transition: color .15s;
}
.back-link:hover { color: var(--text); }

.post-detail-card {
  margin-bottom: 0;
}
.about-title {
  margin-top: 0;
  font-family: var(--ff);
}
.post-detail-card:hover {
  box-shadow: none;
  transform: none;
}
/* Static timestamp variant (no hover underline) */
.post-time-static, .post-time-static .post-time { cursor: default; }
.post-time-static:hover .post-time { color: var(--text-muted); text-decoration: none; }
.post-time-static:hover::before { opacity: 0.55; }

@media (max-width: 480px) {
  .post { padding: 1.25rem; }
}

/* ── Empty state ── */
.empty-state { text-align: center; padding: 5rem 0; color: var(--text-muted); }
.empty-emoji { font-size: 3rem; margin-bottom: .75rem; }
.empty-text  { font-size: 1rem; }
.empty-text a { color: var(--accent); text-decoration: none; }
.empty-text a:hover { text-decoration: underline; }

/* ── Pagination ── */
.pagination {
  margin-top: 2rem;
  padding-top: 2rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 1rem;
  align-items: center;
}

.pagination-info {
  text-align: center;
}

.page-count {
  font-size: 0.875rem;
  color: var(--text-secondary);
  font-weight: 500;
}

.pagination-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  justify-content: center;
}

/* Make pagination buttons more visible */
.pagination .btn-ghost {
  color: var(--text-btn);
  border-color: var(--border-pagination);
  background: var(--bg-input);
}

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

.btn.disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}

.page-numbers {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  margin: 0 0.5rem;
}

.page-number {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2rem;
  height: 2rem;
  padding: 0 0.5rem;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-btn);
  text-decoration: none;
  border-radius: 6px;
  transition: background 0.15s, color 0.15s;
}

.page-number:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.page-number.active {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.page-ellipsis {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2rem;
  height: 2rem;
  color: var(--text-muted);
  font-weight: 500;
}

/* Responsive pagination */
@media (max-width: 640px) {
  .pagination {
    margin-top: 1.5rem;
    padding-top: 1.5rem;
  }

  .pagination-controls {
    gap: 0.375rem;
  }

  /* Hide first/last buttons on mobile */
  .pagination-controls > a:first-child,
  .pagination-controls > a:last-child,
  .pagination-controls > span:first-child,
  .pagination-controls > span:last-child {
    display: none;
  }

  .page-numbers {
    margin: 0 0.25rem;
  }

  .page-number {
    min-width: 1.75rem;
    height: 1.75rem;
    font-size: 0.8rem;
  }
}

/* ── Compose ── */
.page-heading { font-size: 1.25rem; font-weight: 700; margin: 0 0 1.5rem; text-wrap: balance; }
.compose-form { display: flex; flex-direction: column; gap: 1.25rem; }

.form-section {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: 12px; padding: 1.25rem;
  transition: background-color .2s, border-color .2s;
}

.section-label {
  display: block; font-size: .72rem; font-weight: 600;
  text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-muted); margin-bottom: .875rem;
}

/* emoji search */
.emoji-search {
  width: 100%; padding: .5rem .75rem;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: .9rem; margin-bottom: .875rem;
  background: var(--bg-input); color: var(--text);
  outline: none; transition: border-color .15s;
}
.emoji-search:focus { border-color: var(--border-hover); }

/* emoji grid */
.emoji-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(52px, 1fr));
  gap: 4px; max-height: 310px; overflow-y: auto; padding-right: 2px;
  scrollbar-width: thin; scrollbar-color: var(--scrollbar) transparent;
}
.emoji-grid::-webkit-scrollbar { width: 4px; }
.emoji-grid::-webkit-scrollbar-thumb { background: var(--scrollbar); border-radius: 2px; }

.emoji-item {
  display: flex; align-items: center; justify-content: center;
  padding: 6px; border-radius: 8px;
  border: 2px solid transparent; cursor: pointer;
  transition: border-color .1s, background .1s;
}
.emoji-item:hover { background: var(--bg-hover); }
.emoji-item:has(input:checked) { border-color: var(--accent); background: var(--accent-bg); }
.emoji-item input[type="radio"] { position: absolute; opacity: 0; width: 0; height: 0; }
.emoji-item img { width: 38px; height: 38px; display: block; }

/* message */
.message-textarea {
  width: 100%; padding: .625rem .75rem;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 1rem; font-family: var(--ff-serif);
  resize: vertical; min-height: 96px;
  background: var(--bg-input); color: var(--text);
  outline: none; transition: border-color .15s;
}
.message-textarea:focus { border-color: var(--border-hover); }

.char-count { text-align: right; font-size: .8rem; color: var(--text-muted); margin-top: .375rem; }
.char-count.near  { color: var(--warning); }
.char-count.over  { color: var(--danger); }

/* preview */
.preview-box {
  display: flex; align-items: center; gap: 1rem;
  padding: 1rem; background: var(--bg-preview); border-radius: 8px; min-height: 88px;
}
.preview-emoji   { width: 60px; height: 60px; object-fit: contain; flex-shrink: 0; }
.preview-message { font-family: var(--ff-serif); font-size: 1rem; line-height: 1.5; word-break: break-word; }
.preview-message.ph { color: var(--text-muted); font-style: italic; }

.form-actions { display: flex; gap: .75rem; align-items: center; }

/* error */
.error-msg {
  background: var(--error-bg); color: var(--error-text);
  border: 1px solid var(--error-border); border-radius: 8px;
  padding: .75rem 1rem; font-size: .9rem; margin-bottom: .25rem;
}

/* ── Login ── */
.login-wrap {
  max-width: 360px; margin: 4rem auto; padding: 2rem;
  background: var(--bg-card); border: 1px solid var(--border); border-radius: 16px;
  transition: background-color .2s, border-color .2s;
}
.login-wrap h1 { font-size: 1.25rem; margin: 0 0 1.5rem; text-align: center; }

.form-field { margin-bottom: 1rem; }
.form-field label { display: block; font-size: .875rem; font-weight: 500; margin-bottom: .375rem; color: var(--text-btn); }
.form-field input {
  width: 100%; padding: .5rem .75rem;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 1rem; outline: none; transition: border-color .15s;
  background: var(--bg-input); color: var(--text);
}
.form-field input:focus { border-color: var(--accent); }

/* ── Markdown Toolbar ── */
.markdown-toolbar {
  display: flex;
  gap: 0.25rem;
  padding: 0.5rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 8px 8px 0 0;
  flex-wrap: wrap;
}

.markdown-btn {
  padding: 0.375rem 0.625rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-btn);
  cursor: pointer;
  transition: background 0.1s, border-color 0.1s;
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
}

.markdown-btn:hover {
  background: var(--bg-hover);
  border-color: var(--border-hover);
}

.markdown-btn:active {
  background: var(--border);
}

.markdown-btn-icon {
  font-style: normal;
  font-size: 0.9rem;
}

/* Adjust textarea to connect with toolbar */
.message-textarea.with-toolbar {
  border-radius: 0 0 8px 8px;
  border-top: none;
}

/* Markdown rendered content styles */
.post-message strong {
  font-weight: 600;
}

.post-message em {
  font-style: italic;
}

.post-message code {
  background: var(--bg-code);
  padding: 0.125rem 0.375rem;
  border-radius: 3px;
  font-family: var(--ff-mono);
  font-size: 0.9em;
  color: var(--code-text);
}

.post-message a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid var(--accent-border-light);
  transition: border-color 0.15s;
}

.post-message a:hover {
  border-bottom-color: var(--accent);
}

.post-message del {
  text-decoration: line-through;
  opacity: 0.7;
}

.post-message p {
  margin: 0 0 1rem;
}

.post-message p:last-child {
  margin-bottom: 0;
}

.post-message ul,
.post-message ol {
  margin: 0.5rem 0;
  padding-left: 1.5rem;
}

.post-message ul {
  list-style-type: disc;
}

.post-message ol {
  list-style-type: decimal;
}

.post-message li {
  margin: 0.25rem 0;
}

.post-message li:first-child {
  margin-top: 0;
}

.post-message li:last-child {
  margin-bottom: 0;
}

.post-message blockquote {
  margin: 0.75rem 0;
  padding: 0.5rem 0 0.5rem 1rem;
  border-left: 3px solid var(--accent);
  background: var(--bg-blockquote);
  font-style: italic;
  color: var(--text-btn);
}

.post-message blockquote p {
  margin: 0;
}

.post-message blockquote p:last-child {
  margin-bottom: 0;
}

/* Preview blockquote styles (matches post-message) */
.preview-message blockquote {
  margin: 0.75rem 0;
  padding: 0.5rem 0 0.5rem 1rem;
  border-left: 3px solid var(--accent);
  background: var(--bg-blockquote);
  font-style: italic;
  color: var(--text-btn);
}

.preview-message blockquote p {
  margin: 0;
}

.preview-message blockquote p:last-child {
  margin-bottom: 0;
}

/* ── Responsive: sidebar collapses to top bar ── */
@media (max-width: 64rem) {
  .layout {
    flex-direction: column;
    gap: 1.5rem;
    padding: 1.5rem;
  }

  .sidebar {
    width: 100%;
    max-height: none;
    position: static;
    padding: 1rem 1.25rem;
    display: grid;
    grid-template-columns: 1fr auto;
    column-gap: 1rem;
    row-gap: 0.5rem;
    align-items: center;
  }

  .container {
    max-width: none;
    width: 100%;
  }

  .sidebar-top,
  .sidebar-bottom {
    display: contents;
  }

  .site-title { grid-column: 1; grid-row: 1; }
  .sidebar-nav { grid-column: 2; grid-row: 1; justify-self: end; }
  .sidebar-top .site-description { grid-column: 1; grid-row: 2; margin: 0; }
  .footer-nav { grid-column: 2; grid-row: 2; justify-self: end; margin-top: 0; }
}

@media (max-width: 480px) {
  .layout { padding: 1rem; gap: 1rem; }
  .post { gap: 1rem; }
  .post-emoji-img { width: 58px; height: 58px; }
  .emoji-item img { width: 32px; height: 32px; }

  .sidebar {
    grid-template-columns: 1fr;
    justify-items: center;
    text-align: center;
    row-gap: 0.5rem;
  }

  .site-title,
  .sidebar-nav,
  .site-description,
  .footer-nav {
    grid-column: 1;
    justify-self: center;
  }

  .site-title { grid-row: 1; }
  .site-description { grid-row: 2; }
  .sidebar-nav { grid-row: 3; flex-wrap: wrap; justify-content: center; }
  .footer-nav { grid-row: 4; justify-content: center; }

  .footer-link {
    font-size: 0.75rem;
  }
}
