/* ---------- Theme tokens ---------- */
:root,
:root[data-theme="dark"] {
  --bg: #16181c;
  --surface: #1f2228;
  --surface-2: #272b33;
  --border: #2e333c;
  --text: #e6e8eb;
  --text-2: #9aa1ad;
  --text-3: #6c7380;
  --accent: #4fa3f7;
  --up: #ff7a3d;
  --down: #8c9eff;
  --op: #4fa3f7;
  --mod: #3ecf6e;
  --nsfw: #e5484d;
  --read: #8a909b;
  --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  --thread-1: #4fa3f7;
  --thread-2: #3ecf6e;
  --thread-3: #f5b83d;
  --thread-4: #e5484d;
  --thread-5: #b77cf6;
  --thread-6: #2fc6c6;
  color-scheme: dark;
}
:root[data-theme="black"] {
  --bg: #000;
  --surface: #0c0d0f;
  --surface-2: #17191d;
  --border: #22252b;
}
:root[data-theme="light"] {
  --bg: #f4f5f7;
  --surface: #fff;
  --surface-2: #eceef2;
  --border: #dde0e6;
  --text: #16181c;
  --text-2: #5b6270;
  --text-3: #8a909b;
  --accent: #1a73d9;
  --up: #e8590c;
  --down: #5468ff;
  --op: #1a73d9;
  --mod: #1f9d4c;
  --read: #8a909b;
  --shadow: 0 8px 30px rgba(0, 0, 0, .18);
  --thread-1: #1a73d9;
  --thread-2: #1f9d4c;
  --thread-3: #c98500;
  --thread-4: #d6383d;
  --thread-5: #8b4fd6;
  --thread-6: #128f8f;
  color-scheme: light;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}
body.no-scroll { overflow: hidden; }
a { color: var(--accent); text-decoration: none; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, select { font: inherit; color: var(--text); }
[hidden] { display: none !important; }
.muted { color: var(--text-2); }
.small { font-size: 13px; }
.center { text-align: center; }
.spacer { flex: 1; }

.icon { display: inline-flex; width: 22px; height: 22px; flex: none; }
.icon svg { width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 42px; height: 42px; border-radius: 50%; color: var(--text-2); flex: none;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn.small { width: 30px; height: 30px; }
.icon-btn.small .icon { width: 16px; height: 16px; }
.star-btn.on { color: #f5b83d; }
.star-btn.on svg { fill: currentColor; }

.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 9px 16px; border-radius: 8px;
  background: var(--accent); color: #fff; font-weight: 600;
}
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.ghost { background: var(--surface-2); color: var(--text); }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 2px;
  min-height: 52px; padding: 0 4px; padding-top: env(safe-area-inset-top);
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar-title {
  flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start;
  padding: 4px 6px; text-align: left;
}
.topbar-main { font-weight: 700; font-size: 17px; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-sub { font-size: 12px; color: var(--text-2); }
.topbar-actions { display: flex; }

/* ---------- Listing ---------- */
.post-list { background: var(--surface); }
.post-row {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 8px 4px 8px 10px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.post-row:active { background: var(--surface-2); }
.thumb {
  position: relative; flex: none; width: 64px; height: 64px; border-radius: 6px; overflow: hidden;
  background: var(--surface-2); display: flex; align-items: center; justify-content: center; color: var(--text-3);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-icon { width: 26px; height: 26px; }
.thumb-label { font-size: 11px; font-weight: 700; letter-spacing: .03em; }
.thumb-label.nsfw { color: var(--nsfw); }
.badge {
  position: absolute; right: 3px; bottom: 3px; display: inline-flex; align-items: center; gap: 2px;
  padding: 1px 4px; border-radius: 4px; background: rgba(0, 0, 0, .7); color: #fff; font-size: 10px; font-weight: 700;
}
.badge .icon { width: 11px; height: 11px; }
.badge.big { right: 8px; bottom: 8px; font-size: 13px; padding: 3px 7px; }
.badge.big .icon { width: 14px; height: 14px; }
.post-body { flex: 1; min-width: 0; }
.post-title { font-size: 15px; font-weight: 500; line-height: 1.3; overflow-wrap: anywhere; }
.domain { font-size: 12px; color: var(--text-3); font-weight: 400; }
.post-meta {
  display: flex; flex-wrap: wrap; gap: 0 10px; margin-top: 4px; font-size: 12px; color: var(--text-2);
}
.meta-sub { color: var(--accent); }
.meta-author { color: var(--text-3); overflow: hidden; text-overflow: ellipsis; }
.post-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.flair {
  display: inline-block; max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding: 0 5px; border-radius: 3px; background: var(--surface-2); color: var(--text-2);
  font-size: 11px; line-height: 17px; vertical-align: middle;
}
.flair.nsfw { background: var(--nsfw); color: #fff; font-weight: 700; }
.flair.spoiler { background: var(--text-2); color: var(--bg); font-weight: 700; }
.flair.pinned { background: var(--mod); color: #fff; font-weight: 700; }
.post-row.read .post-title { color: var(--read); }
.post-row.liked .meta-score { color: var(--up); font-weight: 600; }
.post-row.disliked .meta-score { color: var(--down); font-weight: 600; }

.votes { display: flex; align-items: center; gap: 2px; color: var(--text-3); }
.votes.vertical { flex-direction: column; gap: 0; margin-top: -2px; }
.vote { display: inline-flex; align-items: center; justify-content: center; width: 36px; height: 30px; }
.vote .icon { width: 20px; height: 20px; }
.votes.liked .vote.up { color: var(--up); }
.votes.liked .vote.up svg { fill: currentColor; }
.votes.disliked .vote.down { color: var(--down); }
.votes.disliked .vote.down svg { fill: currentColor; }
.vote-score { min-width: 28px; text-align: center; font-size: 13px; font-weight: 600; color: var(--text-2); }
.votes.liked .vote-score { color: var(--up); }
.votes.disliked .vote-score { color: var(--down); }

.list-footer { min-height: 60px; display: flex; align-items: center; justify-content: center; padding: 12px; }
.list-end { color: var(--text-3); font-size: 13px; padding: 20px; text-align: center; }
.spinner {
  width: 28px; height: 28px; margin: 20px auto; border-radius: 50%;
  border: 3px solid var(--surface-2); border-top-color: var(--accent); animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.error-box { text-align: center; padding: 16px; color: var(--text-2); }
.notice { margin: 8px; padding: 10px 12px; border-radius: 8px; background: var(--surface-2); font-size: 14px; }

/* ---------- Post header (comments page) ---------- */
.post-header { background: var(--surface); padding: 12px 12px 4px; border-bottom: 1px solid var(--border); }
.post-h-title { font-size: 18px; line-height: 1.3; margin: 0 0 6px; font-weight: 600; overflow-wrap: anywhere; }
.xpost { display: inline-block; margin-top: 6px; font-size: 13px; }
.post-media {
  position: relative; display: block; width: calc(100% + 24px); margin: 10px -12px 0;
  background: #000; min-height: 120px;
}
.post-media img { display: block; width: 100%; max-height: 70vh; object-fit: contain; }
.post-media-placeholder { display: flex; height: 160px; align-items: center; justify-content: center; color: #ccc; }
.play-overlay {
  position: absolute; inset: 0; margin: auto; width: 64px; height: 64px; border-radius: 50%;
  background: rgba(0, 0, 0, .6); color: #fff; display: flex; align-items: center; justify-content: center;
}
.play-overlay .icon { width: 30px; height: 30px; }
.play-overlay svg { fill: currentColor; }
.link-card {
  display: flex; align-items: center; gap: 10px; width: 100%; margin-top: 10px; padding: 8px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); text-align: left;
}
.link-card img { width: 56px; height: 56px; object-fit: cover; border-radius: 4px; flex: none; }
.link-card-icon { width: 28px; height: 28px; color: var(--text-3); margin: 0 8px; }
.link-card-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.link-card-domain { font-weight: 600; font-size: 14px; }
.link-card-url { font-size: 12px; color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.post-header > .md { margin-top: 10px; }
.post-actions { display: flex; align-items: center; gap: 4px; margin-top: 8px; color: var(--text-2); }
.act { display: inline-flex; align-items: center; justify-content: center; gap: 4px; min-width: 40px; height: 36px; color: var(--text-2); border-radius: 6px; }
.act .icon { width: 20px; height: 20px; }
.act.on { color: #f5b83d; }
.act.on svg { fill: currentColor; }
.act-count { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; margin-left: 6px; }
.act-count .icon { width: 18px; height: 18px; }
.single-thread { display: block; padding: 10px 12px; background: var(--surface-2); font-size: 14px; }

/* ---------- Comments ---------- */
.comments { padding-bottom: 40vh; }
.comment { background: var(--surface); border-bottom: 1px solid var(--border); padding: 6px 0 0 10px; }
.comment.nested { border-bottom: 0; border-top: 1px solid var(--border); margin-left: 2px; padding-left: 10px; border-left: 2px solid var(--thread-c); }
.comment > .c-children > .comment:first-child { margin-top: 6px; }
.d0 { --thread-c: var(--thread-1); }
.d1 { --thread-c: var(--thread-2); }
.d2 { --thread-c: var(--thread-3); }
.d3 { --thread-c: var(--thread-4); }
.d4 { --thread-c: var(--thread-5); }
.d5 { --thread-c: var(--thread-6); }
.comment.highlight > .c-body { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.c-head {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0 8px; padding-right: 10px;
  font-size: 12.5px; color: var(--text-3); cursor: pointer; min-height: 22px;
}
.c-author { font-weight: 600; color: var(--text-2); }
.c-author.op { color: var(--op); }
.c-author.mod { color: var(--mod); }
.c-author.admin { color: var(--nsfw); }
.c-author.me { color: var(--up); }
.c-badge { font-size: 10px; font-weight: 700; padding: 0 4px; border-radius: 3px; background: var(--op); color: #fff; }
.c-badge.pinned { background: var(--mod); }
.comment.liked > .c-head .c-score { color: var(--up); }
.comment.disliked > .c-head .c-score { color: var(--down); }
.c-collapsed-count { margin-left: auto; font-weight: 600; color: var(--text-2); }
.c-body { padding: 2px 10px 6px 0; }
.c-actions { display: none; align-items: center; padding: 0 6px 4px 0; }
.comment.show-actions > .c-actions { display: flex; }
.comment.collapsed > .c-body,
.comment.collapsed > .c-actions,
.comment.collapsed > .c-children { display: none; }
.comment.collapsed > .c-head { padding-bottom: 6px; }
.more-link {
  display: block; width: 100%; text-align: left; padding: 8px 10px; font-size: 13px; font-weight: 600;
  color: var(--accent); background: var(--surface); border-top: 1px solid var(--border);
}
.more-link.nested { margin-left: 2px; border-left: 2px solid var(--thread-c); }

/* ---------- Markdown ---------- */
.md { overflow-wrap: anywhere; font-size: 14.5px; line-height: 1.45; }
.md p { margin: 0 0 .6em; }
.md > :last-child { margin-bottom: 0; }
.md blockquote { margin: 0 0 .6em; padding-left: 10px; border-left: 3px solid var(--border); color: var(--text-2); }
.md pre { overflow-x: auto; padding: 8px; background: var(--surface-2); border-radius: 6px; font-size: 13px; }
.md code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .92em; background: var(--surface-2); padding: 0 3px; border-radius: 3px; }
.md pre code { background: none; padding: 0; }
.md ul, .md ol { margin: 0 0 .6em; padding-left: 22px; }
.md h1, .md h2, .md h3, .md h4, .md h5, .md h6 { font-size: 1.05em; margin: .4em 0; }
.md table { display: block; overflow-x: auto; border-collapse: collapse; margin-bottom: .6em; font-size: 13px; }
.md th, .md td { border: 1px solid var(--border); padding: 3px 6px; }
.md hr { border: 0; border-top: 1px solid var(--border); }
.md-spoiler-text { background: var(--text-2); color: transparent; border-radius: 3px; cursor: pointer; }
.md-spoiler-text a { color: transparent; }
.md-spoiler-text.revealed { background: var(--surface-2); color: inherit; }
.md-spoiler-text.revealed a { color: var(--accent); }

/* ---------- Drawer ---------- */
.drawer { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, 0); transition: background .18s; }
.drawer.open { background: rgba(0, 0, 0, .5); }
.drawer-panel {
  position: absolute; top: 0; bottom: 0; left: 0; width: min(320px, 85vw); overflow-y: auto;
  background: var(--surface); box-shadow: var(--shadow);
  padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom);
  transform: translateX(-100%); transition: transform .18s ease-out; overscroll-behavior: contain;
}
.drawer.open .drawer-panel { transform: none; }
.drawer-account {
  display: flex; align-items: center; gap: 10px; padding: 14px 12px; border-bottom: 1px solid var(--border);
}
.drawer-user { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.drawer-go {
  display: flex; align-items: center; gap: 8px; margin: 10px 12px; padding: 0 10px;
  background: var(--surface-2); border-radius: 8px; color: var(--text-3);
}
.drawer-go .icon { width: 18px; height: 18px; }
.drawer-go input { flex: 1; min-width: 0; height: 40px; border: 0; background: none; outline: none; }
.drawer-section { padding: 4px 0; border-bottom: 1px solid var(--border); }
.drawer-heading {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px 4px 16px; font-size: 12px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; color: var(--text-3);
}
.drawer-item { display: flex; align-items: center; gap: 14px; width: 100%; padding: 11px 16px; text-align: left; }
.drawer-item .icon { color: var(--text-2); width: 20px; height: 20px; }
.drawer-item.active, .drawer-sub.active { background: var(--surface-2); }
.drawer-item.active, .drawer-sub.active .drawer-sub-name { color: var(--accent); }
.drawer-sub { display: flex; align-items: center; padding-right: 4px; }
.drawer-sub-name { flex: 1; min-width: 0; padding: 10px 16px; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drawer-sub .icon-btn { width: 38px; height: 38px; }
.drawer-sub .icon-btn .icon { width: 18px; height: 18px; }
.drawer-empty { padding: 6px 16px 10px; font-size: 13px; color: var(--text-3); }

/* ---------- Bottom sheet ---------- */
.sheet-backdrop { position: fixed; inset: 0; z-index: 60; background: rgba(0, 0, 0, .5); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 480px; max-height: 80vh; overflow-y: auto; background: var(--surface);
  border-radius: 14px 14px 0 0; padding: 6px 0 calc(10px + env(safe-area-inset-bottom));
}
.sheet-title { padding: 12px 18px 8px; font-size: 13px; font-weight: 700; color: var(--text-3); text-transform: uppercase; letter-spacing: .05em; }
.sheet-item { display: flex; justify-content: space-between; width: 100%; padding: 13px 18px; text-align: left; font-size: 16px; }
.sheet-item.active { color: var(--accent); font-weight: 600; }
.sheet-chev { color: var(--text-3); }

/* ---------- Viewer ---------- */
.viewer { position: fixed; inset: 0; z-index: 70; background: #000; color: #fff; }
.viewer-track {
  position: absolute; inset: 0; display: flex; overflow-x: auto; overflow-y: hidden;
  scroll-snap-type: x mandatory; scrollbar-width: none; overscroll-behavior: contain;
}
.viewer-track::-webkit-scrollbar { display: none; }
.viewer-track.locked { overflow: hidden; }
.viewer-slide {
  flex: none; width: 100%; height: 100%; scroll-snap-align: center; scroll-snap-stop: always;
  display: flex; align-items: center; justify-content: center; overflow: hidden; touch-action: pan-x;
}
.viewer-slide img { max-width: 100%; max-height: 100%; object-fit: contain; transform-origin: center; user-select: none; -webkit-user-select: none; }
.viewer-slide video { max-width: 100%; max-height: 100%; width: 100%; }
.viewer-error { color: #aaa; }
.viewer-top {
  position: absolute; top: 0; left: 0; right: 0; display: flex; align-items: center; gap: 4px;
  padding: calc(4px + env(safe-area-inset-top)) 4px 16px;
  background: linear-gradient(rgba(0, 0, 0, .7), transparent);
}
.viewer-top .icon-btn { color: #fff; }
.viewer-title { flex: 1; min-width: 0; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-count { font-size: 14px; font-weight: 600; padding: 0 6px; }
.viewer-caption {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 20px 14px calc(14px + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, rgba(0, 0, 0, .8)); font-size: 14px; pointer-events: none;
}

/* ---------- Settings ---------- */
.settings { padding: 10px; max-width: 640px; margin: 0 auto; }
.set-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; margin-bottom: 12px; }
.set-card h2 { font-size: 15px; margin: 0 0 6px; }
.set-card p { margin: 0 0 10px; font-size: 14px; }
.set-field { display: block; margin-bottom: 10px; font-size: 13px; color: var(--text-2); }
.set-field input {
  display: block; width: 100%; margin-top: 4px; padding: 9px 10px;
  border: 1px solid var(--border); border-radius: 6px; background: var(--surface-2);
}
.set-redirect { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.set-redirect code { flex: 1; min-width: 0; overflow-wrap: anywhere; font-size: 12.5px; padding: 6px 8px; background: var(--surface-2); border-radius: 6px; }
.set-actions { display: flex; gap: 8px; }
.set-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 0; border-top: 1px solid var(--border); font-size: 14px; }
.set-row:first-of-type { border-top: 0; }
.set-row select { padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface-2); }
.set-row input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 80;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none; transition: .2s;
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 8px;
  background: #33373f; color: #fff; font-size: 14px; box-shadow: var(--shadow);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
