/* ---------- Fonts (online) ---------- */
@import url("https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700&family=Inter:wght@400;600&family=Noto+Sans+Arabic:wght@400;600&family=Noto+Kufi+Arabic:wght@600;700&family=IBM+Plex+Sans:wght@400;600&display=swap");

/* ---------- Tokens: strictly grayscale ---------- */
:root {
  --bg: #FFFFFF;
  --card: #FFFFFF;
  --fg: #000000;
  --muted: #555555;
  --border: #DDDDDD;
  --link: #000000;
  --focus: #222222;

  --ctl-h: 40px;        /* unified control height */
  --radius: 8px;

  /* NEW: single source of truth for page horizontal padding */
  --page-pad: 1rem;
}

html.theme-dark {
  --bg: #000000;
  --card: #000000;
  --fg: #FFFFFF;
  --muted: #AAAAAA;
  --border: #333333;
  --link: #FFFFFF;
  --focus: #DDDDDD;
}

/* Respect OS preference ONLY when no explicit theme class is set */
@media (prefers-color-scheme: dark) {
  html:not(.theme-light):not(.theme-dark) {
    --bg: #000000;
    --card: #000000;
    --fg: #FFFFFF;
    --muted: #AAAAAA;
    --border: #333333;
    --link: #FFFFFF;
    --focus: #DDDDDD;
  }
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Inter", system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  color: var(--fg);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Arabic fragments: rely on .rtl wrapper (your markup sets this per section) */
.rtl,
.rtl *:not(code):not(pre) {
  font-family: "Noto Sans Arabic", "Inter", system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}

img { max-width: 100%; height: auto; border: 0; }
a { color: var(--link); text-decoration: underline; }
a:hover { text-decoration: none; color: #666; }
html.theme-dark a:hover,
html:not(.theme-light) a:hover { color: #CCC; }

/* Accessibility: focus only when keyboarding */
:where(a, button, input, textarea, select):focus-visible {
  outline: 2px dotted var(--focus);
  outline-offset: 2px;
}
:where(a, button, input, textarea, select):focus:not(:focus-visible) {
  outline: none;
}
:where(a, button).icon-btn, :where(button.btn) { -webkit-tap-highlight-color: transparent; }

.visually-hidden {
  position: absolute !important;
  height: 1px; width: 1px;
  overflow: hidden; clip: rect(1px,1px,1px,1px);
  white-space: nowrap; clip-path: inset(50%);
}

/* Hide toggle icons while JS is bootstrapping theme to avoid wrong first icon */
html.theme-booting #themeToggle .icon { visibility: hidden; }

/* RTL helper for fragments only */
.rtl { direction: rtl; text-align: right; }

/* ---------- Fixed Header ---------- */
.site-header {
  position: fixed; inset: 0 0 auto 0; z-index: 1000;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.site-header .inner {
  max-width: 1000px;
  margin: 0 auto;
  padding: .65rem var(--page-pad); /* use shared page padding */
  display: flex; align-items: center; gap: .8rem;
  flex-wrap: nowrap;
}
.brand {
  margin: 0;
  font-family: "Playfair Display", Georgia, "Times New Roman", serif;
  font-size: clamp(1.25rem, 2.2vw, 1.8rem);
  line-height: 1.05;
  color: var(--fg);
  letter-spacing: .2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 52vw;
}
.brand a { color: inherit; text-decoration: none; display: inline-block; }
.header-spacer { flex: 1 1 auto; min-width: 0; }

/* ---------- Search (flat, borderless icon inside) ---------- */
.search {
  display: flex; align-items: center; gap: .4rem;
  height: var(--ctl-h);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0 .4rem 0 .5rem;
  min-width: 180px;
}
.search input[type="search"] {
  border: none; outline: none; background: transparent; color: var(--fg);
  width: min(44vw, 320px); font-size: .98rem; height: 100%;
  min-width: 90px;
}
.search input::placeholder { color: var(--muted); }

/* Icon in search: no hover bg; pointer only */
.search .btn,
.search .icon-btn {
  border: none;
  background: transparent;
  width: var(--ctl-h);
  height: var(--ctl-h);
  padding: 0;
  cursor: pointer;
}
.search .btn:hover,
.search .icon-btn:hover { background: transparent; }
.search .btn:active,
.search .icon-btn:active { background: transparent; }

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--fg);
  padding: .42rem .7rem;
  border-radius: var(--radius);
  font-size: .95rem;
  cursor: pointer;
}
.btn:hover { background: #F2F2F2; }
html.theme-dark .btn:hover { background: #111111; }
.btn:active { background: #EAEAEA; }
html.theme-dark .btn:active { background: #1A1A1A; }

/* Icon-only button (square) */
.icon-btn {
  width: var(--ctl-h);
  height: var(--ctl-h);
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* SVG icons inherit text color */
.icon {
  width: 18px;
  height: 18px;
  fill: currentColor;
  stroke: currentColor;
  stroke-width: 1.25;
}

/* THEME ICONS (action-based):
   - Light mode shows MOON (click → dark)
   - Dark  mode shows SUN  (click → light)
*/
.icon-sun  { display: none; }
.icon-moon { display: inline; }
html.theme-dark .icon-sun  { display: inline; }
html.theme-dark .icon-moon { display: none; }
html.theme-light .icon-sun  { display: none; }
html.theme-light .icon-moon { display: inline; }

/* ---------- Single Language Toggle Button (Ab / أب) ---------- */
.lang-cycle {
  width: var(--ctl-h);
  height: var(--ctl-h);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: "Playfair Display", Georgia, "Times New Roman", serif;
  font-weight: 700;
  font-size: 1rem;
}
.lang-cycle .lang-cycle-label { line-height: 1; }

/* Arabic label variant (set lang="ar" on the span for the NEXT language) */
.lang-cycle .lang-cycle-label[lang="ar"] {
  font-family: "Noto Kufi Arabic", "Noto Sans Arabic", "Inter", system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  font-weight: 700;
  font-size: 1.1rem; /* slightly larger */
  letter-spacing: 0;
}

/* ---------- Layout ---------- */
.wrap {
  max-width: 1000px;
  margin: 0 auto;
  padding: calc(56px + 1rem) var(--page-pad) 2rem; /* use shared page padding */
}

/* ---------- Cards: default (LTR previews, image left) ---------- */
.card {
  display: grid;
  grid-template-columns: 180px 1fr;
  grid-template-areas: "img body";
  gap: 1rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem;
  margin: 1.1rem 0;
  position: relative;      /* for full-card link overlay */
  cursor: pointer;
  min-height: 180px;
}
.card.noimg { grid-template-columns: 1fr; grid-template-areas: "body"; }
.thumb-link { grid-area: img; position: relative; z-index: 2; }
.content {
  grid-area: body;
  position: relative; z-index: 2;
  display: flex; flex-direction: column;
  min-height: 140px;               /* ensures room to pin tags to bottom */
}

/* Full-card clickable overlay (keeps inner links clickable) */
.card-link {
  position: absolute; inset: 0; border-radius: inherit;
  z-index: 1; /* under .content/.thumb-link (z:2) so inner links work */
}

/* Arabic previews only (image on right, text RTL) */
.rtl-cards .card {
  grid-template-columns: 1fr 180px;
  grid-template-areas: "body img";
}
.rtl-cards .content {
  direction: rtl;
  text-align: right;
}

/* Common content styles */
.thumb {
  width: 100%;
  height: 140px;
  object-fit: cover;
  object-position: center;
  aspect-ratio: 16 / 10;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.title { margin: 0 0 .45rem; font-size: 1.18rem; line-height: 1.3; }
.title a { color: inherit; text-decoration: none; }
.title a:hover { text-decoration: none; color: #666; }
html.theme-dark .title a:hover,
html:not(.theme-light) .title a:hover { color: #CCC; }

/* Ensure per-text direction renders naturally */
.title a[dir="rtl"], .snippet[dir="rtl"], .tag[dir="rtl"] { unicode-bidi: plaintext; }
.title a[dir="ltr"], .snippet[dir="ltr"], .tag[dir="ltr"] { unicode-bidi: plaintext; }

/* Submeta group: date + reading time (smaller & faded) */
/* Use IBM Plex Sans for aligned digits (tabular numbers). Fallback to Noto Sans Arabic on Arabic text. */
.submeta { margin: .05rem 0 .6rem; }
.meta, .readtime {
  font-family: "IBM Plex Sans", "Noto Sans Arabic", "Inter", system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  font-size: .82rem;
  color: var(--muted);
  line-height: 1.35;
  margin: 0;
  opacity: .9;
  font-variant-numeric: tabular-nums;
  -moz-font-feature-settings: "tnum" 1;
  -webkit-font-feature-settings: "tnum" 1;
  font-feature-settings: "tnum" 1;
  letter-spacing: .1px;
}

/* Tags: horizontal chips pinned to bottom */
.tags {
  display: flex;
  flex-wrap: wrap;
  gap: .35rem;
  margin-top: auto;
  padding: 0;
  list-style: none;
}
.tag {
  display: inline-flex;
  align-items: center;
  padding: .18rem .45rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: .85rem;
  color: var(--fg);
  background: var(--card);
  text-decoration: none;
  width: fit-content;
}
.tag:hover { text-decoration: none; color: #666; }
html.theme-dark .tag:hover,
html:not(.theme-light) .tag:hover { color: #CCC; }

/* Optional card hover affordance */
.card:hover { filter: brightness(0.98); }
html.theme-dark .card:hover { filter: brightness(1.05); }

/* ---------- Pagination ---------- */
.pagination {
  display: flex; flex-wrap: wrap; gap: .4rem; margin: 1rem 0;
}
.pagination a, .pagination span {
  padding: .35rem .6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  color: var(--fg);
  text-decoration: none;              /* no underline on buttons */
  transition: background .15s ease, border-color .15s ease, transform .02s ease;
}
.pagination a:hover {
  background: #F2F2F2;
  border-color: #CFCFCF;
  text-decoration: none;
}
html.theme-dark .pagination a:hover,
html:not(.theme-light) .pagination a:hover {
  background: #111111;
  border-color: #3A3A3A;
}
.pagination a:active { transform: scale(0.98); }
.pagination .current { background: #F5F5F5; }
html.theme-dark .pagination .current { background: #111111; }

/* ---------- Markdown (post page) ---------- */
.markdown {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.1rem 1.1rem;
}
/* More breathing room below titles on post pages */
.markdown h1 { margin-top: 1.2rem; margin-bottom: 1.1rem; }
.markdown h2 { margin-top: 1.1rem; margin-bottom: .8rem; }
.markdown h3 { margin-top: 1rem;  margin-bottom: .65rem; }
.markdown h1 + * { margin-top: .2rem; }

.markdown h1, .markdown h2, .markdown h3 {
  font-family: "Playfair Display", Georgia, "Times New Roman", serif;
}
.markdown :lang(ar) h1,
.markdown.rtl h1,
.markdown :lang(ar) h2,
.markdown.rtl h2,
.markdown :lang(ar) h3,
.markdown.rtl h3 {
  /* If headings appear in Arabic context, use Arabic UI font for better shaping */
  font-family: "Noto Sans Arabic", "Inter", system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}
.markdown p { line-height: 1.7; color: var(--fg); }
.markdown img { border-radius: 8px; border: 1px solid var(--border); }

/* ==================== Responsive tweaks ==================== */

/* --- Large tablets (<= 1000px) --- */
@media (max-width: 1000px) {
  :root { --page-pad: .8rem; } /* keep header + content aligned */
  .site-header .inner { padding: .6rem var(--page-pad); gap: .7rem; }
  .wrap { padding: calc(56px + .8rem) var(--page-pad) 2rem; }
}

/* --- Tablets (<= 820px) --- */
@media (max-width: 820px) {
  :root { --ctl-h: 42px; }
  .brand { max-width: 46vw; }
  .search input[type="search"] { width: min(38vw, 260px); }
  .card { grid-template-columns: 160px 1fr; }
  .rtl-cards .card { grid-template-columns: 1fr 160px; }
  .thumb { height: 132px; }
}

/* --- Small phones / phablets (<= 600px) --- */
@media (max-width: 600px) {
  :root { --ctl-h: 46px; }
  .site-header .inner {
    gap: .6rem;
    flex-wrap: wrap;
    /* keep left/right exactly like the cards below */
    padding-left: var(--page-pad);
    padding-right: var(--page-pad);
  }
  .brand {
    max-width: 100%;
    flex: 1 1 auto;
  }
  .header-spacer { display: none; }

  /* Row 2 controls: language, search, theme */
  .lang-cycle { order: 2; }
  .search     { order: 3; flex: 1 1 260px; min-width: 0; }
  #themeToggle{ order: 4; }

  .search input[type="search"] {
    width: 100%;
    min-width: 110px;
    font-size: 1rem;
  }

  .wrap { padding-top: calc(56px + 3.1rem); }
  .card { grid-template-columns: 1fr; grid-template-areas: "img" "body"; }
  .rtl-cards .card { grid-template-columns: 1fr; grid-template-areas: "img" "body"; }
  .thumb { height: 180px; aspect-ratio: 16 / 10; }
}

/* --- Very small phones (<= 400px) --- */
@media (max-width: 400px) {
  :root { --ctl-h: 48px; --page-pad: .6rem; } /* tighter but still aligned */
  .site-header .inner { padding: .55rem var(--page-pad); }
  .brand { font-size: 1.2rem; letter-spacing: 0; }
  .search { padding: 0 .35rem 0 .45rem; }
  .icon { width: 17px; height: 17px; }
  .thumb { height: 170px; }
  .title { font-size: 1.05rem; }
}

/* Motion preference */
@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; transition: none !important; }
}

/* ---------- Print ---------- */
@media print {
  .site-header, .pagination, .btn, .search, .lang-cycle { display: none !important; }
  .wrap { padding: 0; }
  .card, .markdown { border-color: #000; }
}

/* ====== Minimal read link (if used anywhere) ====== */
.read {
  display: inline-block;
  margin-top: .45rem;
  text-decoration: underline;
}
.read:hover { text-decoration: none; }
