:root {
  --bg: #ffffff;
  --bg-alt: #f6f7f9;
  --text: #1a1d23;
  --text-dim: #5b6270;
  --border: #e2e5ea;
  --brand: #2563eb;
  --brand-dim: #dbeafe;
  --code-bg: #1e1f26;
  --code-text: #e8e9ee;
  --tip-bg: #eef6ff;
  --tip-border: #93c5fd;
  --warn-bg: #fff7ed;
  --warn-border: #fb923c;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161b;
    --bg-alt: #1b1e25;
    --text: #e7e9ee;
    --text-dim: #a0a6b3;
    --border: #2a2e38;
    --brand: #5b9dff;
    --brand-dim: #1c2b47;
    --code-bg: #0e0f13;
    --code-text: #e8e9ee;
    --tip-bg: #14233f;
    --tip-border: #3b6bb0;
    --warn-bg: #2b2013;
    --warn-border: #b0752f;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Sarabun", "Noto Sans Thai", "Noto Sans", sans-serif;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
}

.layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "topbar topbar"
    "sidebar content";
  min-height: 100vh;
}

.topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--text);
  text-decoration: none;
}

.nav-toggle {
  display: none;
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 1.2rem;
  padding: 4px 10px;
  color: var(--text);
  cursor: pointer;
}

.sidebar {
  grid-area: sidebar;
  border-right: 1px solid var(--border);
  padding: 20px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  height: calc(100vh - 53px);
  position: sticky;
  top: 53px;
  overflow-y: auto;
}

.sidebar a {
  display: block;
  padding: 9px 12px;
  border-radius: 8px;
  color: var(--text-dim);
  text-decoration: none;
  font-size: 0.95rem;
}

.sidebar a:hover {
  background: var(--bg-alt);
  color: var(--text);
}

.sidebar a.active {
  background: var(--brand-dim);
  color: var(--brand);
  font-weight: 600;
}

.content {
  grid-area: content;
  padding: 36px clamp(20px, 5vw, 64px);
  max-width: 900px;
}

article h1 {
  font-size: 1.25rem;
  margin-top: 2.6em;
  color: var(--brand);
  background: var(--brand-dim);
  padding: 10px 16px;
  border-radius: 8px;
}

article > h1:first-of-type {
  font-size: 1.9rem;
  margin-top: 0;
  color: var(--text);
  background: none;
  padding: 0 0 14px;
  border-bottom: 2px solid var(--border);
  border-radius: 0;
}

article h2 {
  font-size: 1.4rem;
  margin-top: 2.4em;
  padding-top: 0.4em;
  border-top: 1px solid var(--border);
}

article h3 {
  font-size: 1.15rem;
  margin-top: 1.8em;
  color: var(--brand);
}

article p, article li {
  color: var(--text);
}

article a {
  color: var(--brand);
}

.lead {
  color: var(--text-dim);
  font-size: 1.05rem;
}

.stack-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 16px 0 28px;
}

.badge {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 14px;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
  margin: 20px 0 32px;
}

.card {
  display: flex;
  gap: 14px;
  align-items: flex-start;
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text);
  background: var(--bg-alt);
  transition: border-color 0.15s, transform 0.15s;
}

.card:hover {
  border-color: var(--brand);
  transform: translateY(-2px);
}

.card-num {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--brand);
  min-width: 32px;
}

.card-body h3 {
  margin: 0 0 6px;
  font-size: 1rem;
  color: var(--text);
}

.card-done {
  margin: 0 0 8px;
  font-size: 0.88rem;
  color: var(--text-dim);
}

.card-time {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-dim);
}

/* Article images */
article img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  margin: 1.2em 0;
  display: block;
}

/* Markdown table styling */
article table {
  border-collapse: collapse;
  width: 100%;
  margin: 1.2em 0;
  font-size: 0.92rem;
  display: block;
  overflow-x: auto;
}

article th, article td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}

article th {
  background: var(--bg-alt);
}

/* Blockquote callouts: 💡 tip / ⚠️ warning / plain */
article blockquote {
  margin: 1.2em 0;
  padding: 12px 16px;
  border-left: 4px solid var(--tip-border);
  background: var(--tip-bg);
  border-radius: 0 8px 8px 0;
}

article blockquote p {
  margin: 0.4em 0;
}

article li:has(> input[type="checkbox"]) {
  list-style: none;
  margin-left: -1.4em;
}

article li input[type="checkbox"] {
  margin-right: 8px;
}

/* Code blocks */
.code-block {
  margin: 1.2em 0;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
}

.code-block-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #0b0c10;
  padding: 6px 12px;
  border-bottom: 1px solid #2a2e38;
}

.code-lang {
  color: #8b93a7;
  font-size: 0.75rem;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.copy-btn {
  background: #262a35;
  color: #d7dae2;
  border: 1px solid #383e4d;
  border-radius: 6px;
  font-size: 0.78rem;
  padding: 3px 10px;
  cursor: pointer;
}

.copy-btn:hover {
  background: #313644;
}

.copy-btn.copied {
  background: #16803c;
  border-color: #16803c;
  color: white;
}

.code-block pre {
  margin: 0;
  padding: 14px 16px;
  background: var(--code-bg);
  overflow-x: auto;
}

.code-block code {
  color: var(--code-text);
  font-family: ui-monospace, "Cascadia Code", Consolas, "Courier New", monospace;
  font-size: 0.88rem;
  line-height: 1.6;
  white-space: pre;
}

/* Inline code */
article :not(pre) > code {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 6px;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 0.88em;
  color: var(--brand);
}

article hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2.4em 0;
}

.page-nav {
  display: flex;
  justify-content: space-between;
  margin-top: 3em;
  padding-top: 1.5em;
  border-top: 1px solid var(--border);
}

.page-nav-btn {
  color: var(--brand);
  text-decoration: none;
  font-weight: 600;
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.page-nav-btn:hover {
  background: var(--bg-alt);
}

@media (max-width: 820px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-areas:
      "topbar"
      "content";
  }
  .nav-toggle { display: inline-block; }
  .sidebar {
    position: fixed;
    top: 53px;
    left: 0;
    bottom: 0;
    width: 240px;
    background: var(--bg);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 20;
    box-shadow: 2px 0 12px rgba(0,0,0,0.15);
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .content { padding: 24px 18px; }
}
