/* ================================================================
   Knoww Press - Premium Book Reader
   Mobile-first, NodeCore/KG theme consistency, Thai support
   ================================================================ */

:root {
  /* Colors - Dark Mode (Default) */
  --bg: #0a0b0f;
  --surface: #13151c;
  --surface2: #1a1d27;
  --border: #252836;
  --accent: #6366f1;
  --accent2: #818cf8;
  --text: #e2e8f0;
  --text-muted: #8892a4;
  --text-dim: #4e5669;
  --green: #22c55e;
  --amber: #f59e0b;
  --red: #ef4444;

  /* Layout */
  --sidebar-w: 280px;
  --reader-max-w: 720px;
  --nav-h: 56px;

  /* Spacing */
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0,0,0,.5);

  /* Animation */
  --tr: .2s cubic-bezier(.4,0,.2,1);

  /* Font Scaling */
  --fs-scale: 1.2;

  /* Safe Area */
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
  --safe-left: env(safe-area-inset-left);
  --safe-right: env(safe-area-inset-right);
}

/* Light Mode */
body.light {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --text-dim: #cbd5e1;
  --shadow: 0 4px 24px rgba(0,0,0,.08);
}

/* ================================================================
   BASE STYLES
   ================================================================ */

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

html {
  font-size: 16px;
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100dvh;
  overflow-x: hidden;
  width: 100%;
  max-width: 100vw;
  transition: background var(--tr), color var(--tr);
}

/* Thai Language Support */
body[lang="th"],
body[lang="th"] * {
  font-family: 'Noto Sans Thai', 'IBM Plex Sans Thai', sans-serif !important;
  line-height: 1.9 !important;
  letter-spacing: 0 !important;
}

/* RTL Support */
body[dir="rtl"] {
  direction: rtl;
  text-align: right;
}

/* Scrollbar */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }
* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }

/* ================================================================
   NAVBAR
   ================================================================ */

.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  height: var(--nav-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 8px rgba(0,0,0,.25);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 max(16px, var(--safe-left));
  padding-right: max(16px, var(--safe-right));
  gap: 12px;
  transition: all var(--tr);
}

.nav-left, .nav-center, .nav-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.nav-left { flex-shrink: 0; }
.nav-center { flex: 1; justify-content: center; overflow: hidden; }
.nav-right { flex-shrink: 0; }

.nav-brand {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -.01em;
  color: var(--text);
  white-space: nowrap;
}
.nav-brand span { color: var(--accent); }

.book-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 300px;
}

/* Progress Bar */
.progress-bar {
  position: fixed;
  top: var(--nav-h);
  left: 0;
  right: 0;
  height: 3px;
  background: var(--surface2);
  z-index: 99;
  opacity: 0;
  transition: opacity var(--tr);
}

.progress-bar.visible { opacity: 1; }

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  width: 0%;
  transition: width .3s ease;
}

/* ================================================================
   BUTTONS
   ================================================================ */

.btn-icon {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--tr);
  flex-shrink: 0;
}

.btn-icon:hover:not(:disabled) {
  background: var(--surface2);
  color: var(--text);
  border-color: var(--text-dim);
}

.btn-icon:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-icon:active:not(:disabled) {
  transform: scale(0.95);
}

/* Theme Toggle Icons */
.theme-icon { display: none; }
body:not(.light):not(.system) .icon-dark { display: block; }
body.light .icon-light { display: block; }
body.system:not(.light) .icon-system { display: block; }

/* Font Controls */
.font-controls {
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn-fs {
  width: 28px;
  height: 28px;
}

.fs-label {
  font-size: 11px;
  color: var(--text-muted);
  min-width: 40px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* Mobile Only Elements */
.mobile-only { display: none; }

@media (max-width: 768px) {
  .mobile-only { display: flex; }
  .book-title { max-width: 150px; }
  .font-controls { display: none !important; }
}

/* ================================================================
   MAIN LAYOUT
   ================================================================ */

.main-layout {
  padding-top: calc(var(--nav-h) + 3px);
  min-height: 100dvh;
  display: flex;
  position: relative;
  overflow-x: hidden;
  width: 100%;
  max-width: 100vw;
}

/* ================================================================
   SIDEBAR (TOC)
   ================================================================ */

.sidebar {
  position: fixed;
  top: calc(var(--nav-h) + 3px);
  left: 0;
  bottom: 0;
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 90;
  transition: transform var(--tr);
}

.sidebar.hidden {
  transform: translateX(-100%);
}

.sidebar-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.sidebar-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -.01em;
}

/* Language Switcher */
.lang-switcher {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.lang-select {
  width: 100%;
  padding: 8px 12px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  transition: all var(--tr);
}

.lang-select:hover {
  border-color: var(--text-dim);
}

.lang-select:focus {
  outline: none;
  border-color: var(--accent);
}

/* TOC List */
.toc-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px;
}

.toc-item {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--tr);
  margin-bottom: 2px;
  border-left: 3px solid transparent;
}

.toc-item:hover {
  background: var(--surface2);
}

.toc-item.active {
  background: var(--surface2);
  border-left-color: var(--accent);
}

.toc-item-title {
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
  line-height: 1.4;
  margin-bottom: 2px;
}

.toc-item.active .toc-item-title {
  color: var(--accent);
}

.toc-item-meta {
  font-size: 11px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 8px;
}

.toc-item-number {
  font-weight: 600;
  color: var(--text-muted);
}

.toc-group {
  margin-bottom: 12px;
}

.toc-group-title {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--text-dim);
  padding: 8px 12px 4px;
}

/* Reading Stats */
.reading-stats {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  flex-shrink: 0;
}

.stat-item {
  text-align: center;
}

.stat-label {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 4px;
}

.stat-value {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

/* Sidebar Overlay (Mobile) */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  z-index: 89;
  opacity: 0;
  transition: opacity var(--tr);
}

.sidebar-overlay.visible {
  opacity: 1;
}

@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
  }

  .sidebar.open {
    transform: translateX(0);
    box-shadow: var(--shadow);
  }

  .sidebar-overlay {
    display: block;
    pointer-events: none;
  }

  .sidebar-overlay.visible {
    pointer-events: all;
  }
}

/* ================================================================
   CONTENT AREA
   ================================================================ */

.content-area {
  flex: 1;
  margin-left: var(--sidebar-w);
  min-height: calc(100dvh - var(--nav-h) - 3px);
  padding-bottom: max(40px, var(--safe-bottom));
  transition: margin-left var(--tr);
  overflow-x: hidden;
  width: 100%;
  max-width: 100%;
}

.content-area.full-width {
  margin-left: 0;
}

@media (max-width: 768px) {
  .content-area {
    margin-left: 0;
  }
}

/* ================================================================
   LIBRARY VIEW
   ================================================================ */

.library-view {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

.library-header {
  text-align: center;
  margin-bottom: 48px;
}

.library-header h1 {
  font-size: clamp(28px, 6vw, 44px);
  font-weight: 500;
  line-height: 1.16;
  letter-spacing: -.025em;
  color: var(--text);
  margin-bottom: 12px;
}

.library-subtitle {
  font-size: 16px;
  color: var(--text-muted);
}

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
}

.book-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: all var(--tr);
  display: flex;
  flex-direction: column;
}

.book-card:hover {
  border-color: var(--text-dim);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

.book-cover {
  width: 100%;
  aspect-ratio: 2/3;
  object-fit: cover;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
}

.book-info {
  padding: 20px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.book-title-text {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
  line-height: 1.3;
}

.book-subtitle-text {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.4;
  margin-bottom: 12px;
  flex: 1;
}

.book-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 12px;
  color: var(--text-dim);
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.book-meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

@media (max-width: 600px) {
  .book-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .library-view {
    padding: 24px 16px;
  }
}

/* ================================================================
   READER VIEW
   ================================================================ */

.reader-view {
  max-width: var(--reader-max-w);
  margin: 0 auto;
  padding: 40px 20px;
  overflow-x: hidden;
  width: 100%;
}

/* Reader Article */
.reader-article {
  font-size: calc(16px * var(--fs-scale));
  line-height: 1.8;
  color: var(--text);
  overflow-wrap: break-word;
  word-wrap: break-word;
}

.reader-article * {
  max-width: 100%;
  box-sizing: border-box;
}

/* Typography */
.reader-article h1 {
  font-size: calc(32px * var(--fs-scale));
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: -.02em;
  color: var(--text);
  margin: 48px 0 24px;
}

.reader-article h2 {
  font-size: calc(24px * var(--fs-scale));
  font-weight: 600;
  line-height: 1.3;
  letter-spacing: -.015em;
  color: var(--text);
  margin: 40px 0 20px;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--border);
}

.reader-article h3 {
  font-size: calc(20px * var(--fs-scale));
  font-weight: 600;
  line-height: 1.4;
  color: var(--text);
  margin: 32px 0 16px;
}

.reader-article h4 {
  font-size: calc(18px * var(--fs-scale));
  font-weight: 600;
  line-height: 1.4;
  color: var(--text);
  margin: 24px 0 12px;
}

.reader-article p {
  margin: 16px 0;
  line-height: 1.8;
}

.reader-article ul,
.reader-article ol {
  margin: 16px 0;
  padding-left: 28px;
}

.reader-article li {
  margin: 8px 0;
  line-height: 1.7;
}

.reader-article blockquote {
  margin: 24px 0;
  padding: 16px 20px;
  border-left: 4px solid var(--accent);
  background: var(--surface2);
  border-radius: var(--radius-sm);
  font-style: italic;
  color: var(--text-muted);
}

.reader-article blockquote p {
  margin: 0;
}

.reader-article code {
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: calc(14px * var(--fs-scale));
  background: var(--surface2);
  padding: 2px 6px;
  border-radius: 4px;
  color: var(--accent2);
}

.reader-article pre {
  margin: 24px 0;
  padding: 20px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow-x: auto;
  line-height: 1.6;
}

.reader-article pre code {
  background: transparent;
  padding: 0;
  font-size: calc(13px * var(--fs-scale));
  color: var(--text);
}

.reader-article img {
  max-width: calc(100vw - 40px) !important;
  width: auto !important;
  height: auto !important;
  display: block;
  border-radius: var(--radius);
  margin: 32px auto;
  border: 1px solid var(--border);
  object-fit: contain;
}

.reader-article a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color var(--tr);
}

.reader-article a:hover {
  border-bottom-color: var(--accent);
}

.reader-article hr {
  margin: 48px 0;
  border: none;
  height: 1px;
  background: var(--border);
}

.reader-article table {
  width: 100%;
  margin: 24px 0;
  border-collapse: collapse;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

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

.reader-article th {
  background: var(--surface2);
  font-weight: 600;
  font-size: calc(14px * var(--fs-scale));
}

.reader-article tr:last-child td {
  border-bottom: none;
}

/* Chapter Navigation */
.chapter-nav {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-top: 64px;
  padding-top: 32px;
  border-top: 1px solid var(--border);
}

.btn-nav {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--tr);
  flex: 1;
  max-width: 200px;
}

.btn-nav:hover:not(:disabled) {
  background: var(--surface2);
  border-color: var(--text-dim);
  transform: translateY(-1px);
}

.btn-nav:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.btn-nav svg {
  flex-shrink: 0;
}

@media (max-width: 600px) {
  .reader-view {
    padding: 24px 16px;
  }

  .reader-article img {
    max-width: calc(100vw - 32px) !important;
    margin: 20px 0;
    border-radius: var(--radius-sm);
  }

  .chapter-nav {
    margin-top: 48px;
  }

  .btn-nav {
    max-width: none;
  }

  .nav-text {
    display: none;
  }
}

/* ================================================================
   UTILITY STATES
   ================================================================ */

.loading-spinner {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 48px 20px;
  color: var(--text-muted);
  font-size: 14px;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.empty-state {
  text-align: center;
  padding: 80px 20px;
  color: var(--text-muted);
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 24px;
  opacity: 0.5;
}

.empty-state h3 {
  font-size: 20px;
  color: var(--text);
  margin-bottom: 8px;
}

.empty-state p {
  font-size: 14px;
  color: var(--text-muted);
}

/* ================================================================
   RESPONSIVE
   ================================================================ */

@media (max-width: 768px) {
  :root {
    --fs-scale: 1;
  }
}

@media (min-width: 769px) and (max-width: 1024px) {
  :root {
    --sidebar-w: 240px;
  }
}
