:root {
  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #20242d;
  --text: #e6e8ec;
  --muted: #9aa3b2;
  --accent: #6ea8fe;
  --accent-2: #b692ff;
  --border: #2a2f3a;
  --header-bg: rgba(15, 17, 21, 0.85);
  --on-accent: #0f1115;
  --radius: 14px;
  --maxw: 960px;
}

:root[data-theme="light"] {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #1a1d24;
  --muted: #5c6470;
  --accent: #2f6fed;
  --accent-2: #7c4dff;
  --border: #e2e6ec;
  --header-bg: rgba(247, 248, 250, 0.85);
  --on-accent: #ffffff;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Inter", "Sarabun", system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.65;
}

a { color: inherit; text-decoration: none; }
.container { width: 100%; max-width: var(--maxw); margin: 0 auto; padding: 0 20px; }
.muted { color: var(--muted); }

/* Header */
.site-header {
  position: sticky; top: 0; z-index: 10;
  background: var(--header-bg);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.header-inner {
  display: flex; align-items: center; justify-content: space-between;
  height: 64px;
}
.brand { font-weight: 700; font-size: 1.15rem; }
.brand span { color: var(--accent); }
.nav { display: flex; gap: 22px; }
.nav a { color: var(--muted); transition: color .15s; }
.nav a:hover { color: var(--text); }

/* Hero */
.hero {
  padding: 64px 0 40px;
  border-bottom: 1px solid var(--border);
}
.hero h1 { font-size: clamp(1.7rem, 3.5vw, 2.3rem); margin: 0 0 10px; font-weight: 650; }
.hero p { color: var(--muted); font-size: 1.05rem; margin: 0; }

/* Sections */
.section-title { font-size: 1.4rem; margin: 40px 0 20px; }

/* Post grid */
.post-grid {
  display: grid; gap: 22px;
  grid-template-columns: repeat(auto-fill, minmax(min(260px, 100%), 1fr));
  padding-bottom: 60px;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: transform .15s, border-color .15s;
}
.card:hover { border-color: #3a4150; }
.card-cover { width: 100%; height: 160px; object-fit: cover; display: block; }
.card-body { padding: 18px; }
.card-body h3 { margin: 6px 0 8px; font-size: 1.15rem; }
.card-body h3 a:hover { color: var(--accent); }
.card-body p { color: var(--muted); margin: 0 0 12px; }

.tags { display: flex; flex-wrap: wrap; gap: 8px; }
.tag {
  font-size: .78rem; color: var(--muted);
  padding: 2px 0;
}
.tag + .tag { margin-left: 0; }

/* Article */
.article-wrap { padding-top: 32px; padding-bottom: 60px; }
.back-link { color: var(--muted); display: inline-block; margin-bottom: 18px; }
.back-link:hover { color: var(--text); }
.article-wrap h1 { font-size: clamp(1.6rem, 3.5vw, 2.4rem); margin: 8px 0 16px; }
.article-cover { width: 100%; border-radius: var(--radius); margin: 12px 0 24px; }
.article-body { font-size: 1.05rem; }
.article-body h2 { margin-top: 32px; }
.article-body code {
  background: var(--surface-2); padding: 2px 6px; border-radius: 6px; font-size: .9em;
}

/* Form */
.form { display: grid; gap: 18px; max-width: 640px; }
.form label { display: grid; gap: 6px; font-size: .95rem; color: var(--muted); }
.form input, .form textarea {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 11px 13px; font: inherit; width: 100%;
}
.form input:focus, .form textarea:focus { outline: 2px solid var(--accent); border-color: transparent; }
.btn {
  justify-self: start;
  background: var(--accent);
  color: var(--on-accent); font-weight: 600;
  border: none; border-radius: 8px;
  padding: 10px 20px; cursor: pointer; font-size: 1rem;
}
.btn:hover { background: #5b97f0; }
.status { min-height: 1.2em; margin: 0; }
.status.ok { color: #62d39a; }
.status.error { color: #ff7b72; }

.btn.ghost {
  background: transparent; color: var(--text);
  border: 1px solid var(--border); font-weight: 500;
}
.btn.ghost:hover { background: var(--surface-2); border-color: #3a4150; }

/* Category filter chips */
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 24px; }
.chip {
  font-size: .9rem; color: var(--muted);
  background: var(--surface); border: 1px solid var(--border);
  padding: 5px 14px; border-radius: 999px;
}
.chip:hover { color: var(--text); }
.chip.active { color: var(--on-accent); background: var(--accent); border-color: var(--accent); }

/* Card meta / category badge */
.card-meta { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; font-size: .82rem; }
.card-cat {
  color: var(--accent); border: 1px solid var(--border);
  padding: 1px 9px; border-radius: 999px; font-size: .78rem;
}

/* Archive / index */
.archive-group { margin-bottom: 32px; }
.archive-group h3 { border-bottom: 1px solid var(--border); padding-bottom: 8px; }
.archive-list { list-style: none; padding: 0; margin: 0; }
.archive-list li {
  display: flex; justify-content: space-between; gap: 14px;
  padding: 8px 0; border-bottom: 1px solid var(--surface-2);
}
.archive-list a:hover { color: var(--accent); }

/* Markdown images inside an article */
.md-img { max-width: 100%; border-radius: 10px; margin: 14px 0; display: block; }

/* Login / narrow pages */
.narrow { max-width: 460px; padding-top: 32px; }
.card-form {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 26px;
}

/* Admin layout */
.admin-grid {
  display: grid; gap: 36px; padding: 28px 20px 60px;
  grid-template-columns: 1fr 320px;
}
.admin-head { display: flex; align-items: center; justify-content: space-between; }
.admin-aside h3 { margin: 24px 0 10px; font-size: 1rem; }
.admin-aside h3:first-child { margin-top: 0; }
.admin-list { list-style: none; padding: 0; margin: 0; }
.admin-list li {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 8px 0; border-bottom: 1px solid var(--surface-2); font-size: .92rem;
}
.admin-list a:hover { color: var(--accent); }
.badge {
  font-size: .7rem; color: #f0c674; border: 1px solid #5a4a2a;
  padding: 0 6px; border-radius: 6px; margin-left: 6px;
}
.link-danger { background: none; border: none; color: #ff7b72; cursor: pointer; font-size: .85rem; }
.link-danger:hover { text-decoration: underline; }
.cat-form { display: flex; gap: 8px; margin-top: 12px; }
.cat-form input { flex: 1; }

/* Editor extras */
.row { display: flex; gap: 16px; align-items: end; flex-wrap: wrap; }
.row > label { flex: 1; min-width: 180px; }
.form select {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 11px 13px; font: inherit; width: 100%;
}
.check { display: flex; flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.check input { width: auto; }
.upload-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.file-btn { cursor: pointer; }
.cover-preview { width: 120px; height: 80px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.editor-toolbar { display: flex; align-items: center; gap: 12px; }
#content.dragging { outline: 2px dashed var(--accent); outline-offset: 2px; }

/* Theme toggle (injected into nav by api.js) */
.theme-toggle {
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  border-radius: 8px; width: 34px; height: 34px; cursor: pointer;
  font-size: 1rem; line-height: 1; display: inline-flex; align-items: center; justify-content: center;
}
.theme-toggle:hover { color: var(--text); border-color: #3a4150; }

/* Tablet / iPad portrait: stack the two-column admin layout */
@media (max-width: 860px) {
  .admin-grid { grid-template-columns: 1fr; }
}

/* About block — centered profile */
.about { padding: 8px 0 8px; }
.about-inner {
  display: flex; flex-direction: column; align-items: center;
  text-align: center; gap: 18px;
}
.about-avatar {
  width: 180px; height: 180px; border-radius: 50%;
  object-fit: cover; border: 1px solid var(--border);
  cursor: zoom-in; transition: transform .15s;
}
.about-avatar:hover { transform: scale(1.03); }
.about-text { min-width: 0; }
.about p { color: var(--muted); max-width: 620px; margin: 0 auto; }
.about-links { display: flex; flex-wrap: wrap; gap: 16px; margin-top: 12px; justify-content: center; }
.about-links a { color: var(--accent); }
.about-links a:hover { text-decoration: underline; }

/* Click-to-enlarge */
.article-cover, .article-body .md-img { cursor: zoom-in; }
.lightbox {
  position: fixed; inset: 0; z-index: 100; display: none;
  align-items: center; justify-content: center; padding: 24px;
  background: rgba(0, 0, 0, .85); cursor: zoom-out;
}
.lightbox.open { display: flex; }
.lightbox img { max-width: 95vw; max-height: 95vh; border-radius: 8px; box-shadow: 0 10px 40px rgba(0,0,0,.5); }

/* Section header with side action */
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.section-head .section-title { margin-bottom: 0; }

/* Search box */
.search { margin: 0; }
.search input {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 999px;
  padding: 8px 16px; font: inherit; min-width: 220px;
}
.search input:focus { outline: 2px solid var(--accent); border-color: transparent; }

/* Pagination */
.pager { display: flex; gap: 8px; justify-content: center; margin: 28px 0 40px; }
.page-btn {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  width: 38px; height: 38px; cursor: pointer; font: inherit;
}
.page-btn:hover { border-color: #3a4150; }
.page-btn.active { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }

/* Project card links */
.card-links { display: flex; gap: 14px; margin-top: 12px; font-size: .9rem; }
.card-links a { color: var(--accent); }
.card-links a:hover { text-decoration: underline; }

/* Clickable cards (whole card is a link) */
a.card-link { display: block; color: inherit; text-decoration: none; }
a.card-link:hover h3 { color: var(--accent); }

/* Collapsible archive accordion */
details.archive-group { border-bottom: 1px solid var(--border); padding: 6px 0; margin-bottom: 0; }
details.archive-group > summary {
  cursor: pointer; list-style: none;
  font-size: 1.1rem; font-weight: 600; padding: 10px 4px;
  display: flex; align-items: center; gap: 8px;
}
details.archive-group > summary::-webkit-details-marker { display: none; }
details.archive-group > summary::before {
  content: '▸'; color: var(--muted); transition: transform .15s; display: inline-block;
}
details.archive-group[open] > summary::before { transform: rotate(90deg); }
/* indent posts as sub-items */
details.archive-group .archive-list { padding-left: 22px; margin: 4px 0 10px; }

/* Detail layout: content + sticky table-of-contents */
.detail-layout { display: flex; gap: 36px; align-items: flex-start; padding: 18px 0 60px; }
#article { flex: 1; min-width: 0; }
#article h1 { font-size: clamp(1.6rem, 3.5vw, 2.4rem); margin: 8px 0 16px; }
.toc-side { width: 230px; flex-shrink: 0; position: sticky; top: 84px; }
.toc {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 16px; font-size: .9rem;
}
.toc > summary { cursor: pointer; font-weight: 600; color: var(--text); }
.toc-list { list-style: none; margin: 10px 0 0; padding: 0; display: flex; flex-direction: column; gap: 7px; }
.toc-list a { color: var(--muted); }
.toc-list a:hover { color: var(--accent); }
.toc-list .toc-h2 { padding-left: 12px; }
.toc-list .toc-h3 { padding-left: 24px; }
.detail-actions { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 28px; }

/* anchor jumps clear the sticky header */
html { scroll-behavior: smooth; }
#article h1, #article h2, #article h3 { scroll-margin-top: 84px; }

/* Contact */
.contact { padding-top: 8px; padding-bottom: 50px; }

/* Turnstile containers reserve space while the widget loads */
#turnstile-widget, #contact-turnstile { min-height: 65px; display: flex; align-items: center; }
#contact-turnstile { margin: 4px 0; }

/* Responsive */
/* Tablet / iPad portrait: TOC moves above the article instead of beside it */
@media (max-width: 860px) {
  .detail-layout { flex-direction: column; gap: 18px; }
  .toc-side { width: 100%; position: static; order: -1; }
}
/* Phones */
@media (max-width: 560px) {
  .header-inner { height: auto; min-height: 56px; padding: 8px 0; flex-wrap: wrap; gap: 6px 14px; }
  .nav { gap: 14px; font-size: .95rem; }
  .hero { padding: 44px 0 32px; }
  .section-head { flex-direction: column; align-items: flex-start; gap: 10px; }
  .search input { width: 100%; min-width: 0; }
  .row { flex-direction: column; gap: 14px; align-items: stretch; }
  .admin-grid { padding: 20px 16px 50px; gap: 24px; }
  .admin-projects .admin-grid, .container { padding-left: 16px; padding-right: 16px; }
  .about-avatar { width: 140px; height: 140px; }
  .post-grid { gap: 16px; }
  .lightbox { padding: 12px; }
}

/* Footer */
.site-footer {
  border-top: 1px solid var(--border);
  padding: 28px 0; margin-top: 40px;
  color: var(--muted); font-size: .9rem;
}
