/* =============================================
   เว็บเขียนนิยาย — Main Stylesheet v2
   ✨ Light/Dark Theme + Rich Editor + Responsive
   ============================================= */

/* ===== CSS Variables ===== */
:root {
    /* Spacing */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 16px;
    --space-lg: 24px;
    --space-xl: 32px;
    --space-2xl: 48px;
    --space-3xl: 64px;
    /* Radius */
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 18px;
    --radius-xl: 24px;
    --radius-full: 9999px;
    /* Animation */
    --transition: 0.25s ease;
    --transition-slow: 0.4s ease;
    /* Typography */
    --font-main: 'Sarabun', 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    /* Layout */
    --container-max: 1200px;
    --container-sm: 720px;
    --navbar-height: 64px;
}

/* ===== Light Theme (SiamComic Purple/Yellow) ===== */
html, [data-theme="light"] {
    --bg-primary: #f0f0f5;
    --bg-secondary: #ffffff;
    --bg-card: #ffffff;
    --bg-card-hover: #f5f0fa;
    --bg-input: #eef0f5;
    --bg-hero: linear-gradient(135deg, #4b0082 0%, #6a0dad 40%, #8e44ad 70%, #b266ff 100%);
    --text-primary: #333333;
    --text-secondary: #555555;
    --text-muted: #888888;
    --text-heading: #4b0082;
    --accent: #6a0dad;
    --accent-hover: #8e44ad;
    --accent-glow: rgba(106, 13, 173, 0.25);
    --accent-gradient: linear-gradient(135deg, #4b0082, #8e44ad);
    --accent-secondary: #FF3399;
    --success: #00b894;
    --warning: #fdde24;
    --error: #e17055;
    --info: #0984e3;
    --border: rgba(75, 0, 130, 0.08);
    --border-light: rgba(75, 0, 130, 0.12);
    --shadow-sm: 0 2px 8px rgba(75, 0, 130, 0.06);
    --shadow-md: 0 4px 16px rgba(75, 0, 130, 0.08);
    --shadow-lg: 0 8px 32px rgba(75, 0, 130, 0.12);
    --shadow-glow: 0 4px 20px rgba(106, 13, 173, 0.25);
    --navbar-bg: linear-gradient(135deg, #4b0082, #6a0dad, #8e44ad);
    --footer-bg: linear-gradient(135deg, #4b0082, #2d004d);
    --footer-text: rgba(255,255,255,0.7);
}

/* ===== Dark Theme (SiamComic Dark) ===== */
[data-theme="dark"] {
    --bg-primary: #0f0f1a;
    --bg-secondary: #1a1a2e;
    --bg-card: #1e1e35;
    --bg-card-hover: #2d1b50;
    --bg-input: #16162b;
    --bg-hero: linear-gradient(135deg, #1a0030 0%, #2d004d 50%, #4b0082 100%);
    --text-primary: #e2e2f0;
    --text-secondary: #b0a0d0;
    --text-muted: #7a6a98;
    --text-heading: #ffffff;
    --accent: #b266ff;
    --accent-hover: #d4a0ff;
    --accent-glow: rgba(178, 102, 255, 0.3);
    --accent-gradient: linear-gradient(135deg, #8e44ad, #b266ff);
    --accent-secondary: #FF3399;
    --success: #34d399;
    --warning: #fdde24;
    --error: #fb7185;
    --info: #60a5fa;
    --border: rgba(178, 102, 255, 0.1);
    --border-light: rgba(178, 102, 255, 0.16);
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.5);
    --shadow-glow: 0 4px 20px rgba(178, 102, 255, 0.3);
    --navbar-bg: linear-gradient(135deg, #1a0030, #2d004d);
    --footer-bg: #0a0014;
    --footer-text: #7a6a98;
}

/* ===== Reset ===== */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html { font-size: 16px; scroll-behavior: smooth; }
body {
    font-family: var(--font-main);
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.7;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
    transition: background 0.3s ease, color 0.3s ease;
}
a { color: var(--accent); text-decoration: none; transition: color var(--transition); }
a:hover { color: var(--accent-hover); }
img { max-width: 100%; height: auto; display: block; }
.container { max-width: var(--container-max); margin: 0 auto; padding: 0 var(--space-lg); }
.container-sm { max-width: var(--container-sm); }

/* ===== Navbar ===== */
.navbar {
    position: sticky;
    top: 0;
    z-index: 1000;
    background: var(--navbar-bg);
    border-bottom: none;
    height: var(--navbar-height);
    transition: background 0.3s ease;
    padding-top: env(safe-area-inset-top);
    box-shadow: 0 4px 20px rgba(75, 0, 130, 0.3);
}
.nav-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 100%;
}
.nav-logo {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    font-size: 1.3rem;
    font-weight: 700;
    color: #ffffff;
    text-decoration: none;
    flex-shrink: 0;
}
.nav-logo:hover { color: #ffec79; }
.logo-icon { font-size: 1.5rem; }
.logo-img { height: 32px; width: 32px; object-fit: contain; border-radius: var(--radius-sm); }

/* Desktop nav */
.nav-desktop {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    flex: 1;
    margin-left: var(--space-lg);
}
.nav-spacer { flex: 1; }
.nav-link {
    padding: var(--space-sm) var(--space-md);
    color: rgba(255,255,255,0.85);
    font-size: 0.95rem;
    font-weight: 500;
    border-radius: var(--radius-sm);
    transition: all var(--transition);
    white-space: nowrap;
}
.nav-link:hover { color: #ffffff; background: rgba(255,255,255,0.15); }

/* Theme Toggle */
.nav-icon-btn {
    background: none;
    border: 1px solid rgba(255,255,255,0.25);
    border-radius: var(--radius-full);
    cursor: pointer;
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    transition: all var(--transition);
    flex-shrink: 0;
}
.nav-icon-btn:hover { border-color: #ffec79; background: rgba(255,255,255,0.15); }

/* Notification Bell */
.nav-notif-btn {
    position: relative;
    text-decoration: none;
    color: var(--text-primary);
}
.notif-bell { font-size: 1.1rem; line-height: 1; }
.notif-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    background: #e84393;
    color: #fff;
    font-size: 0.65rem;
    font-weight: 700;
    border-radius: 99px;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    border: 2px solid var(--navbar-bg, #fff);
    animation: notifPulse 2s ease-in-out infinite;
}
@keyframes notifPulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.15); }
}

.icon-sun, .icon-moon { line-height: 1; }
html:not([data-theme="dark"]) .icon-moon,
[data-theme="light"] .icon-moon { display: none !important; }
html:not([data-theme="dark"]) .icon-sun,
[data-theme="light"] .icon-sun { display: inline !important; }
[data-theme="dark"] .icon-sun { display: none !important; }
[data-theme="dark"] .icon-moon { display: inline !important; }

/* Language Toggle */
.nav-lang-btn {
    width: 38px; height: 38px;
    border-radius: var(--radius-full);
    border: 1px solid rgba(255,255,255,0.25);
    display: flex; align-items: center; justify-content: center;
    color: rgba(255,255,255,0.85);
    font-size: 0.75rem; font-weight: 700;
    color: var(--text-secondary);
    transition: all var(--transition);
    text-decoration: none; flex-shrink: 0;
}
.nav-lang-btn:hover { border-color: #ffec79; color: #ffec79; }

.nav-write-btn { margin: 0 var(--space-xs); }

/* User Dropdown (Desktop) */
.nav-dropdown { position: relative; }
.nav-dropdown-toggle {
    display: flex; align-items: center; gap: var(--space-xs);
    background: none; border: none; cursor: pointer;
    font-family: inherit; font-size: inherit;
    color: rgba(255,255,255,0.85); padding: var(--space-sm);
}
.dropdown-arrow { font-size: 0.6rem; }
.user-avatar-small { font-size: 1.2rem; }
.nav-dropdown-menu {
    position: absolute; right: 0; top: calc(100% + 8px);
    background: var(--bg-card); border: 1px solid var(--border-light);
    border-radius: var(--radius-md); min-width: 180px;
    padding: var(--space-sm) 0; box-shadow: var(--shadow-lg);
    display: none; animation: fadeDown 0.2s ease;
}
.nav-dropdown-menu.show { display: block; }
@keyframes fadeDown {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: translateY(0); }
}
.dropdown-item {
    display: block; padding: var(--space-sm) var(--space-md);
    color: var(--text-secondary); font-size: 0.9rem; transition: all var(--transition);
}
.dropdown-item:hover { background: rgba(108,92,231,0.08); color: var(--text-heading); }
.dropdown-item-danger { color: var(--error); }
.dropdown-item-danger:hover { background: rgba(255,71,87,0.08); }
.dropdown-divider { border: none; border-top: 1px solid var(--border); margin: var(--space-xs) 0; }

/* ===== Mobile Navbar Elements ===== */
.nav-mobile-actions {
    display: none;
    align-items: center;
    gap: var(--space-sm);
}
.nav-hamburger {
    display: flex; flex-direction: column; justify-content: center;
    align-items: center; width: 44px; height: 44px;
    background: none; border: 1px solid rgba(255,255,255,0.25);
    border-radius: var(--radius-sm); cursor: pointer;
    gap: 5px; padding: 10px; transition: all var(--transition);
}
.nav-hamburger:hover, .nav-hamburger.active { border-color: #ffec79; }
.nav-hamburger span {
    display: block; width: 20px; height: 2.5px;
    background: #ffffff; border-radius: 2px;
    transition: all 0.3s ease; transform-origin: center;
}
.nav-hamburger.active span:nth-child(1) { transform: rotate(45deg) translate(5px, 5px); }
.nav-hamburger.active span:nth-child(2) { opacity: 0; transform: scaleX(0); }
.nav-hamburger.active span:nth-child(3) { transform: rotate(-45deg) translate(5px, -5px); }

/* ===== Mobile Dropdown Menu ===== */
.mobile-menu {
    display: none;
    position: fixed; top: var(--navbar-height); left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.45); z-index: 998;
    opacity: 0; visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}
.mobile-menu.open { opacity: 1; visibility: visible; }
.mobile-menu-inner {
    background: var(--bg-secondary);
    border-bottom: 3px solid var(--accent);
    box-shadow: 0 8px 32px rgba(0,0,0,0.2);
    max-height: calc(100vh - var(--navbar-height) - 20px);
    overflow-y: auto; -webkit-overflow-scrolling: touch;
    padding: var(--space-lg) var(--space-md);
    transform: translateY(-20px); opacity: 0;
    transition: transform 0.3s ease, opacity 0.3s ease;
}
.mobile-menu.open .mobile-menu-inner { transform: translateY(0); opacity: 1; }

.mobile-user-info {
    display: flex; align-items: center; gap: var(--space-md);
    padding: var(--space-md) var(--space-lg);
    background: var(--bg-card); border-radius: var(--radius-md);
    margin-bottom: var(--space-md); border: 1px solid var(--border);
}
.mobile-user-avatar { font-size: 2rem; }
.mobile-user-name { font-size: 1.1rem; font-weight: 600; color: var(--text-heading); }

.mobile-nav-links { display: flex; flex-direction: column; gap: 2px; }
.mobile-nav-link {
    display: flex; align-items: center; gap: var(--space-md);
    padding: 14px var(--space-lg);
    color: var(--text-primary); font-size: 1.05rem; font-weight: 500;
    text-decoration: none; border-radius: var(--radius-sm);
    transition: all var(--transition); min-height: 52px;
}
.mobile-nav-link:hover { background: rgba(108,92,231,0.08); color: var(--accent); }
.mobile-nav-link.mobile-highlight {
    background: rgba(108,92,231,0.1); color: var(--accent); font-weight: 700;
    border: 1px solid rgba(108,92,231,0.2);
}
.mobile-nav-link.mobile-danger { color: var(--error); }
.mobile-nav-link.mobile-danger:hover { background: rgba(225,112,85,0.08); }

.mobile-divider { border: none; border-top: 1px solid var(--border); margin: var(--space-sm) 0; }
.mobile-bottom { padding: var(--space-md) 0 var(--space-sm); text-align: center; }
.mobile-lang-btn {
    display: inline-flex; align-items: center; gap: var(--space-sm);
    padding: 10px 24px; color: var(--text-secondary); font-size: 0.9rem;
    border: 1px solid var(--border); border-radius: var(--radius-full);
    transition: all var(--transition); text-decoration: none;
}
.mobile-lang-btn:hover { border-color: var(--accent); color: var(--accent); }

@media (max-width: 768px) {
    .nav-desktop { display: none !important; }
    .nav-mobile-actions { display: flex !important; }
    .mobile-menu { display: block !important; }
}

/* ===== Main Content ===== */
.main-content {
    min-height: calc(100vh - var(--navbar-height) - 200px);
}


/* ===== Hero Section ===== */
.hero {
    background: var(--bg-hero);
    padding: var(--space-3xl) 0 var(--space-2xl);
    text-align: center;
    position: relative;
    overflow: hidden;
}
.hero::before {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(ellipse at 50% 0%, rgba(255,255,255,0.15) 0%, transparent 60%);
    pointer-events: none;
}
.hero-content { position: relative; z-index: 1; }
.hero-title {
    font-size: 3rem;
    font-weight: 700;
    color: #fff;
    line-height: 1.2;
    margin-bottom: var(--space-lg);
}
.hero-highlight {
    background: linear-gradient(135deg, #ffd700, #ff6b6b, #c471f5);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}
.hero-subtitle {
    font-size: 1.15rem;
    color: rgba(255,255,255,0.85);
    margin-bottom: var(--space-xl);
    line-height: 1.8;
}
.hero-actions { display: flex; gap: var(--space-md); justify-content: center; flex-wrap: wrap; }
.hero .btn-primary { background: #fdde24; color: #4b0082; box-shadow: 0 4px 16px rgba(0,0,0,0.15); font-weight: 700; }
.hero .btn-primary:hover { background: #ffec79; color: #4b0082; }
.hero .btn-outline { border-color: rgba(255,255,255,0.5); color: #fff; }
.hero .btn-outline:hover { background: rgba(255,255,255,0.15); border-color: #fff; color: #fff; }

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-sm);
    padding: 10px 22px;
    font-family: var(--font-main);
    font-size: 0.95rem;
    font-weight: 600;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition);
    text-decoration: none;
    white-space: nowrap;
}
.btn:hover { transform: translateY(-1px); }
.btn-primary { background: var(--accent-gradient); color: #fff; box-shadow: var(--shadow-glow); }
.btn-primary:hover { box-shadow: 0 6px 24px var(--accent-glow); color: #fff; }
.btn-outline { background: transparent; color: var(--text-primary); border: 1.5px solid var(--border-light); }
.btn-outline:hover { border-color: var(--accent); color: var(--accent); }
.btn-ghost { background: transparent; color: var(--text-secondary); }
.btn-ghost:hover { color: var(--text-heading); }
.btn-sm { padding: 6px 14px; font-size: 0.85rem; }
.btn-lg { padding: 14px 32px; font-size: 1.05rem; }
.btn-block { width: 100%; }
.btn-bookmark { background: var(--bg-input); color: var(--text-secondary); border: 1px solid var(--border); }
.btn-bookmark.bookmarked { background: rgba(232,67,147,0.12); color: var(--accent-secondary); border-color: var(--accent-secondary); }

/* ===== Sections ===== */
.section { padding: var(--space-2xl) 0; }
.section-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--space-lg); }
.section-title { font-size: 1.5rem; font-weight: 700; color: var(--text-heading); }
.section-link { font-size: 0.9rem; color: var(--accent); font-weight: 500; }
.page-header { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: var(--space-xl); gap: var(--space-md); flex-wrap: wrap; }
.page-title { font-size: 1.8rem; font-weight: 700; color: var(--text-heading); }
.page-subtitle { color: var(--text-secondary); margin-top: var(--space-xs); }

/* ===== Categories ===== */
.categories-bar { display: flex; gap: var(--space-sm); flex-wrap: wrap; justify-content: center; }
.category-tag {
    padding: 6px 16px;
    background: var(--bg-card);
    color: var(--text-secondary);
    border-radius: var(--radius-full);
    font-size: 0.85rem;
    font-weight: 500;
    border: 1px solid var(--border);
    transition: all var(--transition);
    box-shadow: var(--shadow-sm);
}
.category-tag:hover { border-color: var(--accent); color: var(--accent); }

/* ===== AI Section Banner ===== */
.ai-section-banner {
    display: flex;
    align-items: center;
    gap: 10px;
    background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
    color: #fff;
    border-radius: 10px;
    padding: 10px 16px;
    margin-bottom: var(--space-lg);
    font-size: 0.88rem;
}
.ai-banner-icon { font-size: 1.3rem; flex-shrink: 0; }
.ai-banner-text { line-height: 1.4; }

/* ===== Story Grid ===== */
.story-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: var(--space-lg);
}
.story-card {
    background: var(--bg-card);
    border-radius: var(--radius-md);
    overflow: hidden;
    border: 1px solid var(--border);
    transition: all var(--transition);
    box-shadow: var(--shadow-sm);
}
.story-card:hover { transform: translateY(-5px); border-color: var(--accent); box-shadow: var(--shadow-glow); }
.story-card-link { text-decoration: none; color: inherit; display: block; }
.story-card-link:hover { color: inherit; }
.story-card-cover { position: relative; aspect-ratio: 2/3; overflow: hidden; background: var(--bg-input); }
.story-card-cover img { width: 100%; height: 100%; object-fit: cover; transition: transform var(--transition-slow); }
.story-card:hover .story-card-cover img { transform: scale(1.06); }

.cover-placeholder, .cover-placeholder-lg, .cover-placeholder-sm {
    display: flex; align-items: center; justify-content: center;
    width: 100%; height: 100%;
    background: linear-gradient(135deg, var(--bg-input), var(--bg-card));
    font-size: 3rem;
}
.cover-placeholder-lg { font-size: 5rem; }
.cover-placeholder-sm { font-size: 2rem; }

.story-card-badge {
    position: absolute; top: 8px; right: 8px; padding: 2px 8px;
    border-radius: var(--radius-full); font-size: 0.7rem; font-weight: 600;
}
.story-card-body { padding: var(--space-md); }
.story-card-title {
    font-size: 0.95rem; font-weight: 600; color: var(--text-heading); margin-bottom: 4px;
    display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.story-card-author { font-size: 0.8rem; color: var(--text-muted); margin-bottom: 6px; }
.story-card-category {
    display: inline-block; padding: 1px 8px;
    background: rgba(108,92,231,0.1); color: var(--accent);
    border-radius: var(--radius-full); font-size: 0.72rem; font-weight: 500; margin-bottom: 8px;
}
.story-card-meta { display: flex; gap: var(--space-md); font-size: 0.78rem; color: var(--text-muted); }

/* ===== Badges ===== */
.badge { display: inline-block; padding: 3px 10px; border-radius: var(--radius-full); font-size: 0.75rem; font-weight: 600; }
.badge-sm { padding: 2px 8px; font-size: 0.7rem; }
.badge-xs { padding: 1px 6px; font-size: 0.65rem; }
.badge-ongoing { background: rgba(0,184,148,0.12); color: var(--success); }
.badge-completed { background: rgba(9,132,227,0.12); color: var(--info); }
.badge-paused { background: rgba(253,203,110,0.25); color: #d68a00; }
.badge-draft { background: rgba(253,203,110,0.25); color: #d68a00; }
.badge-published { background: rgba(0,184,148,0.12); color: var(--success); }
.badge-ai     { background: rgba(79,70,229,0.12); color: #4f46e5; }
.badge-mature { background: rgba(220,38,38,0.10); color: #dc2626; }

/* Badge row ในหน้า detail */
.story-badge-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: var(--space-sm); }

/* Flag badges บนการ์ด (มุมซ้ายบน — ใต้กันเรียงลงมา) */
.story-card-flag {
    position: absolute; left: 6px;
    font-size: 0.72rem; font-weight: 700; line-height: 1;
    padding: 2px 7px; border-radius: var(--radius-full);
    pointer-events: none; backdrop-filter: blur(2px);
}
.flag-mature { background: rgba(220,38,38,0.88); color: #fff; top: 8px; }
.flag-ai     { background: rgba(79,70,229,0.88); color: #fff; top: 30px; }

/* Notice box ในหน้า detail */
.story-notices { display: flex; flex-direction: column; gap: 8px; margin-bottom: var(--space-lg); }
.story-notice {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 14px; border-radius: var(--radius-md);
    font-size: 0.875rem; line-height: 1.4;
}
.story-notice-ai     { background: rgba(79,70,229,0.08); border: 1px solid rgba(79,70,229,0.25); color: #4338ca; }
.story-notice-mature { background: rgba(220,38,38,0.06); border: 1px solid rgba(220,38,38,0.25); color: #b91c1c; }

/* ===== Update List ===== */
.update-list { display: flex; flex-direction: column; gap: var(--space-sm); }
.update-item {
    display: flex; gap: var(--space-md); padding: var(--space-md);
    background: var(--bg-card); border-radius: var(--radius-md);
    border: 1px solid var(--border); transition: all var(--transition);
    color: inherit; text-decoration: none; box-shadow: var(--shadow-sm);
}
.update-item:hover { border-color: var(--accent); background: var(--bg-card-hover); color: inherit; }
.update-cover { width: 50px; height: 70px; border-radius: var(--radius-sm); overflow: hidden; flex-shrink: 0; }
.update-cover img { width: 100%; height: 100%; object-fit: cover; }
.update-info { flex: 1; min-width: 0; }
.update-story-title { font-size: 0.95rem; font-weight: 600; color: var(--text-heading); margin-bottom: 2px; }
.update-chapter { font-size: 0.85rem; color: var(--text-secondary); margin-bottom: 4px; }
.update-meta { font-size: 0.78rem; color: var(--text-muted); }

/* ===== Filter Bar ===== */
.filter-bar { display: flex; justify-content: space-between; align-items: flex-start; gap: var(--space-md); margin-bottom: var(--space-xl); flex-wrap: wrap; }
.filter-categories { display: flex; gap: var(--space-sm); flex-wrap: wrap; flex: 1; }
.filter-tag {
    padding: 5px 14px; background: var(--bg-card); color: var(--text-secondary);
    border-radius: var(--radius-full); font-size: 0.82rem; font-weight: 500;
    border: 1px solid var(--border); transition: all var(--transition);
}
.filter-tag:hover, .filter-tag.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.filter-sort { display: flex; align-items: center; gap: var(--space-sm); flex-shrink: 0; }
.filter-label { font-size: 0.85rem; color: var(--text-muted); }

/* ===== Story Detail ===== */
.story-detail-header { display: flex; gap: var(--space-xl); margin-bottom: var(--space-xl); }
.story-detail-cover { width: 220px; flex-shrink: 0; border-radius: var(--radius-md); overflow: hidden; box-shadow: var(--shadow-md); }
.story-detail-cover img { width: 100%; aspect-ratio: 2/3; object-fit: cover; }
.story-detail-info { flex: 1; }
.story-detail-title { font-size: 2rem; font-weight: 700; color: var(--text-heading); margin: var(--space-sm) 0; line-height: 1.3; }
.story-detail-author { color: var(--text-secondary); margin-bottom: var(--space-sm); }
.story-detail-category { display: inline-block; padding: 3px 12px; background: rgba(108,92,231,0.1); color: var(--accent); border-radius: var(--radius-full); font-size: 0.82rem; margin-bottom: var(--space-md); }
.story-detail-stats { display: flex; gap: var(--space-lg); color: var(--text-muted); font-size: 0.88rem; margin-bottom: var(--space-lg); flex-wrap: wrap; }
.story-detail-actions { display: flex; gap: var(--space-sm); flex-wrap: wrap; }

.story-synopsis { background: var(--bg-card); border-radius: var(--radius-md); padding: var(--space-lg); margin-bottom: var(--space-xl); border: 1px solid var(--border); box-shadow: var(--shadow-sm); }
.synopsis-title { font-size: 1.1rem; font-weight: 600; margin-bottom: var(--space-md); color: var(--text-heading); }
.synopsis-content { color: var(--text-secondary); line-height: 1.9; }

.chapter-list-section { background: var(--bg-card); border-radius: var(--radius-md); padding: var(--space-lg); border: 1px solid var(--border); box-shadow: var(--shadow-sm); }
.chapter-list-title { font-size: 1.2rem; font-weight: 600; margin-bottom: var(--space-md); color: var(--text-heading); }
.chapter-count { font-size: 0.9rem; color: var(--text-muted); font-weight: 400; }
.chapter-list { display: flex; flex-direction: column; }
.chapter-list-item { display: flex; align-items: center; gap: var(--space-md); padding: var(--space-md); border-bottom: 1px solid var(--border); color: var(--text-primary); transition: all var(--transition); }
.chapter-list-item:last-child { border-bottom: none; }
.chapter-list-item:hover { background: rgba(108,92,231,0.04); color: var(--accent); }
.chapter-number { flex-shrink: 0; font-size: 0.85rem; color: var(--text-muted); width: 80px; }
.chapter-title { flex: 1; font-weight: 500; }
.chapter-date { font-size: 0.8rem; color: var(--text-muted); flex-shrink: 0; }

/* ===== Reading Page ===== */
.reading-section { padding: var(--space-xl) 0 var(--space-3xl); }
.reading-container { max-width: var(--container-sm); margin: 0 auto; padding: 0 var(--space-lg); }
.reading-header { text-align: center; margin-bottom: var(--space-xl); padding-bottom: var(--space-lg); border-bottom: 1px solid var(--border); }
.reading-back { font-size: 0.88rem; color: var(--text-muted); display: inline-block; margin-bottom: var(--space-sm); }
.reading-story-title a { font-size: 1rem; color: var(--text-secondary); font-weight: 500; }
.reading-author { font-size: 0.85rem; color: var(--text-muted); }
.reading-chapter-header { text-align: center; margin-bottom: var(--space-xl); }
.reading-chapter-number { display: inline-block; padding: 3px 14px; background: rgba(108,92,231,0.1); color: var(--accent); border-radius: var(--radius-full); font-size: 0.85rem; font-weight: 600; margin-bottom: var(--space-sm); }
.reading-chapter-title { font-size: 1.6rem; font-weight: 700; color: var(--text-heading); margin-bottom: var(--space-sm); }
.reading-date { font-size: 0.82rem; color: var(--text-muted); }

/* Reading content — renders Rich Text HTML */
.reading-content {
    font-size: 1.15rem;
    line-height: 2;
    color: var(--text-primary);
    margin-bottom: var(--space-3xl);
    word-wrap: break-word;
    overflow-wrap: break-word;
}
.reading-content p {
    margin-bottom: 0.8em;
    text-indent: 2em;
}
.reading-content p.ql-align-center,
.reading-content p.ql-align-right,
.reading-content p.ql-align-justify { text-indent: 0; }
.reading-content h1, .reading-content h2, .reading-content h3 {
    color: var(--text-heading);
    margin: 1.2em 0 0.6em;
    text-indent: 0;
}
.reading-content blockquote {
    border-left: 4px solid var(--accent);
    padding: var(--space-md) var(--space-lg);
    margin: var(--space-lg) 0;
    background: rgba(108,92,231,0.05);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    font-style: italic;
    text-indent: 0;
}
.reading-content ul, .reading-content ol {
    margin: var(--space-md) 0;
    padding-left: 2em;
    text-indent: 0;
}
.reading-content pre {
    background: var(--bg-input);
    padding: var(--space-md);
    border-radius: var(--radius-sm);
    overflow-x: auto;
    font-family: 'Courier New', monospace;
    font-size: 0.9rem;
    margin: var(--space-lg) 0;
    text-indent: 0;
}
.reading-content img {
    max-width: 100%;
    border-radius: var(--radius-sm);
    margin: var(--space-lg) auto;
}
/* Quill alignment classes */
.reading-content .ql-align-center { text-align: center; }
.reading-content .ql-align-right { text-align: right; }
.reading-content .ql-align-justify { text-align: justify; }
.reading-content .ql-indent-1 { padding-left: 3em; }
.reading-content .ql-indent-2 { padding-left: 6em; }
.reading-content .ql-indent-3 { padding-left: 9em; }

.reading-nav { display: flex; justify-content: space-between; align-items: center; gap: var(--space-md); padding-top: var(--space-xl); border-top: 1px solid var(--border); }
.reading-nav-btn { min-width: 140px; text-align: center; }
.reading-end { color: var(--text-muted); font-style: italic; font-size: 0.9rem; }

/* ===== Auth Pages ===== */
.auth-section { padding: var(--space-2xl) 0; display: flex; align-items: center; min-height: calc(100vh - var(--navbar-height) - 200px); }
.auth-card { max-width: 460px; margin: 0 auto; background: var(--bg-card); border-radius: var(--radius-lg); padding: var(--space-xl); border: 1px solid var(--border); box-shadow: var(--shadow-lg); }
.auth-header { text-align: center; margin-bottom: var(--space-xl); }
.auth-title { font-size: 1.8rem; font-weight: 700; color: var(--text-heading); margin-bottom: var(--space-xs); }
.auth-subtitle { color: var(--text-secondary); }
.auth-footer { text-align: center; margin-top: var(--space-lg); color: var(--text-secondary); font-size: 0.9rem; }
.auth-link { color: var(--accent); font-weight: 600; }

/* ===== Forms ===== */
.form-card { background: var(--bg-card); border-radius: var(--radius-lg); padding: var(--space-xl); border: 1px solid var(--border); box-shadow: var(--shadow-sm); }
.form-group { margin-bottom: var(--space-lg); }
.form-group-grow { flex: 1; }
.form-label { display: block; margin-bottom: var(--space-sm); font-weight: 600; font-size: 0.9rem; color: var(--text-heading); }
.required { color: var(--error); }
.form-input, .form-select, .form-textarea {
    width: 100%;
    padding: 11px 15px;
    background: var(--bg-input);
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-family: var(--font-main);
    font-size: 0.95rem;
    transition: border-color var(--transition), box-shadow var(--transition);
}
.form-input:focus, .form-select:focus, .form-textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}
.form-select { cursor: pointer; appearance: auto; }
.form-select-sm { padding: 6px 10px; font-size: 0.85rem; width: auto; }
.form-textarea { resize: vertical; min-height: 100px; }
.form-textarea-lg { min-height: 400px; line-height: 1.9; font-size: 1rem; }
.form-input-file { padding: 8px; }
.form-help { font-size: 0.8rem; color: var(--text-muted); margin-top: var(--space-xs); }
.form-row { display: flex; gap: var(--space-md); }
.form-row .form-group { flex: 1; }
.form-actions { display: flex; gap: var(--space-md); margin-top: var(--space-lg); }
.current-cover { margin-bottom: var(--space-md); }
.cover-preview { max-height: 200px; border-radius: var(--radius-sm); border: 1px solid var(--border); }
.form-section-title { font-size: 1.1rem; font-weight: 700; color: var(--text-heading); margin-bottom: var(--space-lg); padding-bottom: var(--space-sm); border-bottom: 2px solid var(--accent); display: inline-block; }
.form-divider { border: none; border-top: 1px solid var(--border); margin: var(--space-lg) 0; }

.status-selector { display: flex; gap: var(--space-md); }
.status-option input { display: none; }
.status-card { display: block; padding: var(--space-md) var(--space-lg); background: var(--bg-input); border: 2px solid var(--border); border-radius: var(--radius-sm); cursor: pointer; transition: all var(--transition); font-weight: 500; }
.status-option input:checked + .status-card { border-color: var(--accent); background: rgba(108,92,231,0.06); }
.status-card:hover { border-color: var(--accent); }

/* ===== Quill Editor Overrides ===== */
#editorContainer {
    min-height: 450px;
    font-family: var(--font-main);
    font-size: 1.05rem;
    line-height: 1.9;
    border: 1.5px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius-sm) var(--radius-sm);
    background: var(--bg-secondary);
    color: var(--text-primary);
}
#editorContainer .ql-editor {
    min-height: 400px;
    padding: var(--space-lg);
}
#editorContainer .ql-editor p {
    margin-bottom: 0.5em;
}
/* Quill toolbar theming */
.ql-toolbar.ql-snow {
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm) var(--radius-sm) 0 0;
    background: var(--bg-card);
    font-family: var(--font-main);
}
.ql-container.ql-snow {
    border: none;
}
[data-theme="dark"] .ql-toolbar.ql-snow {
    border-color: var(--border-light);
}
[data-theme="dark"] .ql-snow .ql-stroke { stroke: var(--text-secondary); }
[data-theme="dark"] .ql-snow .ql-fill { fill: var(--text-secondary); }
[data-theme="dark"] .ql-snow .ql-picker-label { color: var(--text-secondary); }
[data-theme="dark"] .ql-snow .ql-picker-options { background: var(--bg-card); border-color: var(--border-light); }
[data-theme="dark"] .ql-snow .ql-picker-item { color: var(--text-primary); }
[data-theme="dark"] .ql-editor.ql-blank::before { color: var(--text-muted); }

.editor-footer {
    padding: var(--space-sm) var(--space-md);
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius-sm) var(--radius-sm);
}

/* ===== Alerts & Flash ===== */
.alert { padding: var(--space-md); border-radius: var(--radius-sm); margin-bottom: var(--space-lg); font-size: 0.9rem; }
.alert-error { background: rgba(225,112,85,0.1); border: 1px solid rgba(225,112,85,0.3); color: var(--error); }
.alert-success { background: rgba(0,184,148,0.1); border: 1px solid rgba(0,184,148,0.3); color: var(--success); }
.flash { padding: var(--space-md) var(--space-lg); border-radius: var(--radius-sm); margin-bottom: var(--space-lg); font-weight: 500; transition: all 0.3s ease; }
.flash-success { background: rgba(0,184,148,0.1); border: 1px solid rgba(0,184,148,0.3); color: var(--success); }
.flash-error { background: rgba(225,112,85,0.1); border: 1px solid rgba(225,112,85,0.3); color: var(--error); }
.flash-info { background: rgba(9,132,227,0.1); border: 1px solid rgba(9,132,227,0.3); color: var(--info); }

/* ===== Dashboard ===== */
.stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--space-md); margin-bottom: var(--space-xl); }
.stat-card {
    background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius-md);
    padding: var(--space-lg); text-align: center; display: flex; flex-direction: column;
    gap: var(--space-xs); box-shadow: var(--shadow-sm); transition: all var(--transition);
}
.stat-card:hover { box-shadow: var(--shadow-md); transform: translateY(-2px); }
.stat-icon { font-size: 1.8rem; }
.stat-value { font-size: 2rem; font-weight: 700; color: var(--text-heading); }
.stat-label { font-size: 0.85rem; color: var(--text-muted); }
.dashboard-section-title { font-size: 1.2rem; font-weight: 600; color: var(--text-heading); margin-bottom: var(--space-lg); }
.dashboard-stories { display: flex; flex-direction: column; gap: var(--space-md); }
.dashboard-story-card {
    display: flex; gap: var(--space-lg); background: var(--bg-card);
    border: 1px solid var(--border); border-radius: var(--radius-md);
    padding: var(--space-lg); transition: all var(--transition); box-shadow: var(--shadow-sm);
}
.dashboard-story-card:hover { border-color: var(--accent); box-shadow: var(--shadow-md); }
.dashboard-story-cover { width: 80px; height: 110px; border-radius: var(--radius-sm); overflow: hidden; flex-shrink: 0; }
.dashboard-story-cover img { width: 100%; height: 100%; object-fit: cover; }
.dashboard-story-info { flex: 1; min-width: 0; }
.dashboard-story-top { display: flex; align-items: center; gap: var(--space-sm); margin-bottom: var(--space-sm); }
.dashboard-story-title { font-size: 1.1rem; font-weight: 600; }
.dashboard-story-title a { color: var(--text-heading); }
.dashboard-story-title a:hover { color: var(--accent); }
.dashboard-story-meta { display: flex; gap: var(--space-md); flex-wrap: wrap; margin-bottom: var(--space-md); font-size: 0.82rem; color: var(--text-muted); }
.meta-draft { color: var(--warning); }
.dashboard-story-actions { display: flex; gap: var(--space-sm); flex-wrap: wrap; }

/* ===== Chapter Form Layout ===== */
.chapter-form-layout { display: grid; grid-template-columns: 1fr 280px; gap: var(--space-xl); align-items: start; }
.chapter-form-main { min-width: 0; }
.chapter-form-sidebar {
    background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius-md);
    padding: var(--space-lg); position: sticky; top: calc(var(--navbar-height) + 20px);
    max-height: calc(100vh - var(--navbar-height) - 40px); overflow-y: auto; box-shadow: var(--shadow-sm);
}
.sidebar-title { font-size: 1rem; font-weight: 600; color: var(--text-heading); margin-bottom: var(--space-md); }
.sidebar-empty { color: var(--text-muted); font-size: 0.85rem; }
.sidebar-chapter-list { list-style: none; }
.sidebar-chapter-item { display: flex; align-items: center; gap: var(--space-sm); border-bottom: 1px solid var(--border); }
.sidebar-chapter-item:last-child { border-bottom: none; }
.sidebar-chapter-item a { flex: 1; display: flex; flex-direction: column; gap: 2px; padding: var(--space-sm) 0; color: var(--text-secondary); font-size: 0.82rem; min-width: 0; }
.sidebar-chapter-item a:hover { color: var(--accent); }
.sidebar-chapter-item.active a { color: var(--accent); font-weight: 600; }
.sidebar-ch-num { font-size: 0.72rem; color: var(--text-muted); }
.sidebar-ch-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.btn-delete-chapter { background: none; border: none; color: var(--text-muted); cursor: pointer; padding: 4px; font-size: 0.8rem; border-radius: var(--radius-sm); transition: all var(--transition); flex-shrink: 0; }
.btn-delete-chapter:hover { color: var(--error); background: rgba(225,112,85,0.1); }

/* ===== Tabs ===== */
.tabs { display: flex; gap: 0; border-bottom: 2px solid var(--border); margin-bottom: var(--space-xl); }
.tab { padding: var(--space-md) var(--space-lg); color: var(--text-secondary); font-weight: 500; border-bottom: 2px solid transparent; margin-bottom: -2px; transition: all var(--transition); }
.tab:hover { color: var(--text-heading); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ===== Search ===== */
.search-form { margin-bottom: var(--space-xl); }
.search-input-group { display: flex; gap: var(--space-sm); }
.search-input { flex: 1; font-size: 1.1rem; padding: 14px 18px; }
.search-btn { flex-shrink: 0; padding: 14px 28px; }
.search-result-count { color: var(--text-secondary); margin-bottom: var(--space-lg); }

/* ===== Empty State ===== */
.empty-state { text-align: center; padding: var(--space-3xl); color: var(--text-secondary); }
.empty-state-sm { padding: var(--space-xl); }
.empty-icon { font-size: 3rem; display: block; margin-bottom: var(--space-md); }
.empty-state h3 { font-size: 1.2rem; color: var(--text-heading); margin-bottom: var(--space-sm); }
.empty-state p { margin-bottom: var(--space-lg); }

/* ===== Pagination ===== */
.pagination { display: flex; justify-content: center; gap: var(--space-xs); margin-top: var(--space-xl); flex-wrap: wrap; }
.page-btn { padding: 8px 14px; background: var(--bg-card); color: var(--text-secondary); border-radius: var(--radius-sm); font-size: 0.88rem; border: 1px solid var(--border); transition: all var(--transition); box-shadow: var(--shadow-sm); }
.page-btn:hover { border-color: var(--accent); color: var(--accent); }
.page-btn.active { background: var(--accent-gradient); color: #fff; border-color: transparent; }
.page-dots { padding: 8px 6px; color: var(--text-muted); }

/* ===== Writer Setup Steps ===== */
.setup-steps { display: flex; align-items: center; justify-content: center; gap: var(--space-md); margin-bottom: var(--space-xl); padding: var(--space-lg) 0; }
.setup-step { display: flex; align-items: center; gap: var(--space-sm); color: var(--text-muted); font-size: 0.9rem; font-weight: 500; }
.setup-step.done { color: var(--success); }
.setup-step.active { color: var(--accent); font-weight: 700; }
.step-num { width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 0.85rem; font-weight: 700; background: var(--bg-input); border: 2px solid var(--border); transition: all var(--transition); }
.setup-step.done .step-num { background: var(--success); color: #fff; border-color: var(--success); }
.setup-step.active .step-num { background: var(--accent-gradient); color: #fff; border-color: var(--accent); box-shadow: var(--shadow-glow); }
.setup-step-line { width: 40px; height: 2px; background: var(--border); }
.setup-step-line.done { background: var(--success); }

/* ===== Footer ===== */
.footer { background: var(--footer-bg); border-top: 1px solid var(--border); padding: var(--space-2xl) 0 var(--space-lg); margin-top: var(--space-2xl); color: var(--footer-text); }
.footer-grid { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: var(--space-xl); margin-bottom: var(--space-xl); }
.footer-title { font-size: 1.2rem; color: #fff; margin-bottom: var(--space-md); }
.footer-desc { color: var(--footer-text); font-size: 0.88rem; line-height: 1.7; }
.footer-subtitle { font-size: 0.95rem; color: #e0e0e0; margin-bottom: var(--space-md); }
.footer-links { list-style: none; }
.footer-links li { margin-bottom: var(--space-sm); }
.footer-links a { color: var(--footer-text); font-size: 0.88rem; transition: color var(--transition); }
.footer-links a:hover { color: #ffec79; }
.footer-bottom { text-align: center; padding-top: var(--space-lg); border-top: 1px solid rgba(255,255,255,0.08); color: var(--footer-text); font-size: 0.82rem; }

/* ==========================================================
   RESPONSIVE — Mobile / Tablet / Desktop
   ========================================================== */

/* Tablet & smaller desktop */
@media (max-width: 900px) {
    .story-detail-header { flex-direction: column; align-items: center; text-align: center; }
    .story-detail-cover { width: 180px; }
    .story-detail-stats, .story-detail-actions { justify-content: center; }
    .chapter-form-layout { grid-template-columns: 1fr; }
    .chapter-form-sidebar { position: static; max-height: none; }
    .footer-grid { grid-template-columns: 1fr; }
    .stats-grid { grid-template-columns: repeat(2, 1fr); }
    .filter-bar { flex-direction: column; }
    .filter-sort { width: 100%; justify-content: flex-end; }
}

/* Mobile */
@media (max-width: 768px) {
    /* Safe area for notched phones (iPhone X+, etc.) */
    .navbar { padding-top: env(safe-area-inset-top); }
    .footer { padding-bottom: env(safe-area-inset-bottom); }

    /* Hero */
    .hero-title { font-size: 2rem; }
    .hero-subtitle { font-size: 1rem; }

    /* Story grid — 2 columns on mobile */
    .story-grid {
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
        gap: var(--space-md);
    }
    .story-card-body { padding: var(--space-sm) var(--space-md); }
    .story-card-title { font-size: 0.85rem; }
    .story-card-meta { gap: var(--space-sm); font-size: 0.72rem; }

    /* Forms */
    .form-row { flex-direction: column; gap: var(--space-md); }
    .form-card { padding: var(--space-lg); }
    .form-actions { flex-direction: column; }
    .form-actions .btn { width: 100%; }
    .status-selector { flex-direction: column; }
    .form-input, .form-select, .form-textarea {
        font-size: 16px; /* ป้องกัน zoom บน iOS */
    }

    /* Reading */
    .reading-content { font-size: 1.05rem; }
    .reading-chapter-title { font-size: 1.3rem; }
    .reading-nav { flex-direction: column; gap: var(--space-sm); }
    .reading-nav-btn { width: 100%; min-width: auto; }

    /* Search */
    .search-input-group { flex-direction: column; }

    /* Page header */
    .page-header { flex-direction: column; gap: var(--space-sm); }
    .page-title { font-size: 1.4rem; }

    /* Dashboard */
    .dashboard-story-card { flex-direction: column; gap: var(--space-md); }
    .dashboard-story-cover { width: 60px; height: 80px; }
    .dashboard-story-actions { flex-direction: column; }
    .dashboard-story-actions .btn { width: 100%; text-align: center; }

    /* Update list */
    .update-item { flex-direction: row; gap: var(--space-sm); }
    .update-cover { width: 50px; height: 65px; flex-shrink: 0; }

    /* Chapter form sidebar */
    .sidebar-chapter-item a { padding: var(--space-sm) 0; min-height: 40px; justify-content: center; }

    /* Quill editor */
    #editorContainer { min-height: 300px; }
    #editorContainer .ql-editor { min-height: 260px; padding: var(--space-md); font-size: 1rem; }
    .ql-toolbar.ql-snow { overflow-x: auto; white-space: nowrap; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; }

    /* Tabs */
    .tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .tab { white-space: nowrap; flex-shrink: 0; min-height: 44px; display: flex; align-items: center; }

    /* Buttons — touch friendly */
    .btn { min-height: 44px; }
    .btn-sm { min-height: 36px; }

    /* Filter */
    .filter-categories { gap: var(--space-xs); }
    .filter-tag { padding: 6px 12px; font-size: 0.78rem; }

    /* Section spacing */
    .section { padding: var(--space-xl) 0; }
    .section-title { font-size: 1.2rem; }
}

/* Small phones */
@media (max-width: 480px) {
    :root {
        --navbar-height: 56px;
    }
    .hero { padding: var(--space-xl) 0; }
    .hero-title { font-size: 1.5rem; }
    .hero-subtitle { font-size: 0.88rem; }
    .hero-actions { flex-direction: column; align-items: stretch; }
    .hero-actions .btn { width: 100%; }
    .stats-grid { grid-template-columns: 1fr 1fr; gap: var(--space-sm); }
    .stat-card { padding: var(--space-md); }
    .stat-value { font-size: 1.3rem; }
    .stat-icon { font-size: 1.4rem; }
    .auth-card { padding: var(--space-md); margin: 0 var(--space-sm); }
    .auth-title { font-size: 1.4rem; }
    .setup-steps { gap: var(--space-sm); }
    .step-label { display: none; }
    .story-grid { grid-template-columns: repeat(2, 1fr); gap: var(--space-sm); }
    .story-card-body { padding: var(--space-sm); }
    .container { padding: 0 var(--space-md); }
    .reading-container { padding: 0 var(--space-md); }
    .reading-chapter-title { font-size: 1.2rem; }

    /* Story detail mobile */
    .story-detail-cover { width: 140px; }
    .story-detail-title { font-size: 1.3rem; }
    .story-detail-stats { font-size: 0.8rem; gap: var(--space-sm); }

    /* Chapter list compact */
    .chapter-list-item { padding: var(--space-sm) var(--space-md); }
    .chapter-number { width: 60px; font-size: 0.78rem; }

    /* Footer */
    .footer-grid { gap: var(--space-lg); }
    .footer-bottom { font-size: 0.75rem; }

    /* Pagination touch */
    .page-btn { padding: 10px 14px; min-width: 40px; min-height: 40px; }
}

/* ===== Like Button ===== */
.reading-actions {
    text-align: center;
    padding: var(--space-xl) 0;
    margin-bottom: var(--space-lg);
}
.btn-like {
    display: inline-flex;
    align-items: center;
    gap: var(--space-sm);
    padding: 12px 28px;
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: var(--radius-full);
    cursor: pointer;
    font-family: var(--font-main);
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-secondary);
    transition: all var(--transition);
    text-decoration: none;
}
.btn-like:hover {
    border-color: #e84393;
    color: #e84393;
    background: rgba(232, 67, 147, 0.05);
    transform: scale(1.05);
}
.btn-like.liked {
    border-color: #e84393;
    color: #e84393;
    background: rgba(232, 67, 147, 0.08);
}
.like-icon { font-size: 1.3rem; }
.like-count { font-size: 0.9rem; opacity: 0.7; }

/* Reading meta row */
.reading-meta-row {
    display: flex;
    gap: var(--space-md);
    justify-content: center;
    align-items: center;
    margin-top: var(--space-sm);
}
.reading-stat {
    font-size: 0.82rem;
    color: var(--text-muted);
}

/* Chapter stats in story detail */
.chapter-stats {
    display: flex;
    gap: var(--space-sm);
    font-size: 0.78rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

/* ===== Daily Chart ===== */
.daily-chart-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: var(--space-lg);
    margin-bottom: var(--space-xl);
    box-shadow: var(--shadow-sm);
}
.daily-chart {
    display: flex;
    align-items: flex-end;
    justify-content: space-around;
    gap: var(--space-sm);
    height: 160px;
    padding-top: var(--space-md);
}
.chart-bar-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    flex: 1;
}
.chart-bar-value {
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--text-secondary);
}
.chart-bar {
    width: 100%;
    max-width: 40px;
    min-height: 4px;
    background: var(--accent-gradient);
    border-radius: 4px 4px 0 0;
    transition: height 0.4s ease;
}
.chart-bar-label {
    font-size: 0.7rem;
    color: var(--text-muted);
}

/* Stats grid 6 items */
@media (min-width: 901px) {
    .stats-grid { grid-template-columns: repeat(6, 1fr); }
}
@media (max-width: 900px) {
    .stats-grid { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 480px) {
    .stats-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ===== Logo Image ===== */
.logo-img {
    height: 36px;
    width: auto;
    object-fit: contain;
    border-radius: 4px;
}

/* ===== Modal (Type Selection Popup) ===== */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    z-index: 2000;
    align-items: center;
    justify-content: center;
    padding: var(--space-lg);
}
.modal-overlay.show { display: flex; }
.modal-content {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    padding: var(--space-xl);
    max-width: 520px;
    width: 100%;
    position: relative;
    box-shadow: var(--shadow-lg);
    animation: modalIn 0.3s ease;
}
@keyframes modalIn {
    from { opacity: 0; transform: scale(0.9) translateY(20px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}
.modal-close {
    position: absolute;
    top: 12px;
    right: 16px;
    background: none;
    border: none;
    font-size: 1.8rem;
    cursor: pointer;
    color: var(--text-muted);
    line-height: 1;
}
.modal-close:hover { color: var(--text-heading); }
.modal-title {
    font-size: 1.4rem;
    font-weight: 700;
    color: var(--text-heading);
    text-align: center;
    margin-bottom: var(--space-xs);
}
.modal-subtitle {
    text-align: center;
    color: var(--text-secondary);
    margin-bottom: var(--space-xl);
}
.modal-options {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-md);
}
.modal-option {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-sm);
    padding: var(--space-xl) var(--space-lg);
    background: var(--bg-input);
    border: 2px solid var(--border);
    border-radius: var(--radius-md);
    transition: all var(--transition);
    text-decoration: none;
    color: var(--text-primary);
    text-align: center;
}
.modal-option:hover {
    border-color: var(--accent);
    background: rgba(108, 92, 231, 0.05);
    transform: translateY(-4px);
    box-shadow: var(--shadow-glow);
    color: var(--text-heading);
}
.modal-option-icon { font-size: 3rem; }
.modal-option-title { font-size: 1.1rem; font-weight: 700; color: var(--text-heading); }
.modal-option-desc { font-size: 0.8rem; color: var(--text-muted); line-height: 1.5; }

/* ===== Comic Upload Area ===== */
.comic-upload-area { margin-bottom: var(--space-md); }
.comic-upload-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-sm);
    padding: var(--space-2xl);
    border: 2px dashed var(--border-light);
    border-radius: var(--radius-md);
    background: var(--bg-input);
    cursor: pointer;
    transition: all var(--transition);
}
.comic-upload-box:hover {
    border-color: var(--accent);
    background: rgba(108, 92, 231, 0.04);
}
.comic-upload-icon { font-size: 2.5rem; }
.comic-upload-text { font-size: 1rem; font-weight: 600; color: var(--text-heading); }
.comic-upload-hint { font-size: 0.8rem; color: var(--text-muted); text-align: center; }

/* Comic preview list */
.comic-preview-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: var(--space-sm);
    margin-top: var(--space-md);
}
.comic-preview-item {
    position: relative;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}
.comic-preview-item img {
    width: 100%;
    aspect-ratio: 2/3;
    object-fit: cover;
}
.comic-preview-num {
    display: block;
    text-align: center;
    font-size: 0.72rem;
    color: var(--text-muted);
    padding: 2px;
    background: var(--bg-card);
}

/* Comic existing image grid */
.comic-existing-images { margin-bottom: var(--space-lg); }
.comic-image-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: var(--space-sm);
}
.comic-image-item { position: relative; }
.comic-image-label {
    display: block;
    cursor: pointer;
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    transition: all var(--transition);
}
.comic-image-label input { display: none; }
.comic-image-label img { width: 100%; aspect-ratio: 2/3; object-fit: cover; }
.comic-image-delete {
    display: none;
    position: absolute;
    inset: 0;
    background: rgba(231, 76, 60, 0.7);
    color: #fff;
    font-size: 0.85rem;
    font-weight: 600;
    align-items: center;
    justify-content: center;
}
.comic-image-label input:checked ~ .comic-image-delete { display: flex; }
.comic-image-label input:checked ~ img { opacity: 0.4; }
.comic-image-num {
    display: block;
    text-align: center;
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 2px;
}

/* ===== Page Transition: Book Flip Animation ===== */
.page-transition-overlay {
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    z-index: 99998;
    pointer-events: all;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(15,15,26,0.65);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    opacity: 0;
    transition: opacity 0.35s ease;
}
.page-transition-overlay.active { opacity: 1; }
.page-transition-overlay.fade-out { opacity: 0; }

.book-flip {
    width: 100px;
    height: 140px;
    perspective: 800px;
    position: relative;
}
.book-flip .book-cover {
    position: absolute;
    width: 100%;
    height: 100%;
    background: var(--accent-gradient, linear-gradient(135deg, #6c5ce7, #a855f7));
    border-radius: 4px 12px 12px 4px;
    box-shadow: -2px 4px 20px rgba(108,92,231,0.5);
    z-index: 1;
}
.book-flip .book-cover::before {
    content: '📖';
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    font-size: 2rem;
}
.book-flip .book-cover::after {
    content: '';
    position: absolute;
    left: 0; top: 0;
    width: 10px; height: 100%;
    background: rgba(0,0,0,0.2);
    border-radius: 4px 0 0 4px;
}
.book-flip .book-page {
    position: absolute;
    top: 4px; right: 2px;
    width: calc(100% - 12px);
    height: calc(100% - 8px);
    background: #fffef8;
    border-radius: 2px 8px 8px 2px;
    transform-origin: left center;
    box-shadow: 0 1px 6px rgba(0,0,0,0.08);
    backface-visibility: hidden;
    z-index: 2;
}
.book-flip .book-page:nth-child(2) { animation: bookPageFlip 1.4s ease-in-out infinite; z-index: 4; }
.book-flip .book-page:nth-child(3) { animation: bookPageFlip 1.4s ease-in-out 0.2s infinite; z-index: 3; }
.book-flip .book-page:nth-child(4) { animation: bookPageFlip 1.4s ease-in-out 0.4s infinite; z-index: 2; }
.book-flip .book-page .page-line {
    position: absolute;
    left: 12px;
    height: 3px;
    background: #ddd;
    border-radius: 2px;
}
.book-flip .book-page .page-line:nth-child(1) { top: 18px; width: 55%; }
.book-flip .book-page .page-line:nth-child(2) { top: 30px; width: 70%; }
.book-flip .book-page .page-line:nth-child(3) { top: 42px; width: 45%; }
.book-flip .book-page .page-line:nth-child(4) { top: 54px; width: 65%; }
.book-flip .book-page .page-line:nth-child(5) { top: 66px; width: 40%; }

.book-flip-text {
    margin-top: 24px;
    font-size: 0.88rem;
    color: var(--text-muted, #8892a4);
    text-align: center;
    letter-spacing: 0.5px;
}
.book-flip-text::after {
    content: '';
    animation: loadingDots 1.5s infinite;
}

@keyframes bookPageFlip {
    0%, 10%  { transform: rotateY(0deg); }
    35%, 55% { transform: rotateY(-170deg); box-shadow: 5px 1px 10px rgba(0,0,0,0.12); }
    80%, 100% { transform: rotateY(0deg); }
}
@keyframes loadingDots {
    0%   { content: '.'; }
    33%  { content: '..'; }
    66%  { content: '...'; }
}

/* ===== Font Size Controls ===== */
.reading-toolbar {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 8px;
    margin-bottom: var(--space-lg);
    padding: 10px 16px;
    background: var(--bg-card);
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    flex-wrap: wrap;
}
.reading-toolbar-label {
    font-size: 0.82rem;
    color: var(--text-muted);
    margin-right: 4px;
}
.font-size-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 1rem;
    font-weight: 700;
    cursor: pointer;
    transition: all var(--transition);
    font-family: var(--font-main);
}
.font-size-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: rgba(108,92,231,0.06);
}
.font-size-btn:active { transform: scale(0.92); }
.font-size-display {
    font-size: 0.82rem;
    color: var(--text-secondary);
    min-width: 36px;
    text-align: center;
    font-weight: 600;
}

@media (max-width: 768px) {
    .reading-toolbar {
        justify-content: center;
        padding: 8px 12px;
        gap: 6px;
    }
    .font-size-btn { width: 40px; height: 40px; }
}

/* ===== Comic Reader (Webtoon Style) ===== */
.comic-reading-container {
    max-width: 800px;
    margin: 0 auto;
    padding: 0 var(--space-lg);
}
.comic-reader {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0;
    margin-bottom: var(--space-xl);
}
.comic-page {
    width: 100%;
    max-width: 720px;
}
.comic-page img {
    width: 100%;
    height: auto;
    display: block;
}

/* Mobile adjustments */
@media (max-width: 768px) {
    .modal-options { grid-template-columns: 1fr; }
    .comic-reading-container { padding: 0; }
    .comic-page { max-width: 100%; }
    .comic-image-grid { grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); }
    .comic-preview-list { grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); }
}

/* ===== Type Selection (Novel vs Comic) ===== */
.type-selection {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-lg);
    max-width: 600px;
    margin: 0 auto;
}
.type-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-md);
    padding: var(--space-2xl) var(--space-xl);
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: var(--radius-lg);
    transition: all var(--transition);
    text-decoration: none;
    color: var(--text-primary);
    text-align: center;
    box-shadow: var(--shadow-sm);
}
.type-card:hover {
    border-color: var(--accent);
    background: var(--bg-card-hover);
    transform: translateY(-6px);
    box-shadow: var(--shadow-glow);
    color: var(--text-heading);
}
.type-icon { font-size: 4rem; }
.type-title {
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--text-heading);
}
.type-desc {
    font-size: 0.85rem;
    color: var(--text-muted);
    line-height: 1.6;
}

@media (max-width: 480px) {
    .type-selection { grid-template-columns: 1fr; gap: var(--space-md); }
    .type-card { padding: var(--space-xl) var(--space-lg); }
    .type-icon { font-size: 3rem; }
}

/* ===== Bottom Tab Bar (PWA App-like Navigation) ===== */
.bottom-tab-bar {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1001;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    box-shadow: 0 -2px 16px rgba(0,0,0,0.1);
    padding-bottom: env(safe-area-inset-bottom);
}
.bottom-tab-bar-inner {
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 56px;
    max-width: 600px;
    margin: 0 auto;
}
.bottom-tab {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    flex: 1;
    padding: 6px 0;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.68rem;
    font-weight: 500;
    transition: color 0.2s ease;
    -webkit-tap-highlight-color: transparent;
    position: relative;
    background: none;
    border: none;
    cursor: pointer;
    font-family: var(--font-main);
}
.bottom-tab-icon {
    font-size: 1.35rem;
    line-height: 1;
    transition: transform 0.2s ease;
}
.bottom-tab-label {
    line-height: 1;
    letter-spacing: 0.2px;
}
.bottom-tab.active {
    color: var(--accent);
    font-weight: 700;
}
.bottom-tab.active .bottom-tab-icon {
    transform: scale(1.1);
}
.bottom-tab:active .bottom-tab-icon {
    transform: scale(0.9);
}
.bottom-tab .tab-notif-badge {
    position: absolute;
    top: 2px;
    right: calc(50% - 18px);
    background: var(--error, #e74c3c);
    color: #fff;
    font-size: 0.6rem;
    font-weight: 700;
    min-width: 16px;
    height: 16px;
    border-radius: 99px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
    line-height: 1;
}

/* PWA Standalone Mode — แสดง bottom tab bar + ปรับ UI */
@media (display-mode: standalone) {
    .bottom-tab-bar { display: block !important; }
    /* เพิ่มพื้นที่ล่างให้เนื้อหาไม่โดน tab bar บัง */
    .main-content { padding-bottom: calc(56px + env(safe-area-inset-bottom, 0px) + 8px); }
    .footer { padding-bottom: calc(56px + env(safe-area-inset-bottom, 0px) + 16px); }
    /* ซ่อน hamburger menu บน mobile, ใช้ bottom tab แทน */
    .nav-hamburger { display: none !important; }
    /* ซ่อน PWA install banner เพราะติดตั้งแล้ว */
    #pwaInstallBanner { display: none !important; }
}
/* iOS standalone fallback (ไม่รองรับ display-mode media query บางรุ่น) */
html.pwa-standalone .bottom-tab-bar { display: block !important; }
html.pwa-standalone .main-content { padding-bottom: calc(56px + env(safe-area-inset-bottom, 0px) + 8px); }
html.pwa-standalone .footer { padding-bottom: calc(56px + env(safe-area-inset-bottom, 0px) + 16px); }
html.pwa-standalone .nav-hamburger { display: none !important; }
html.pwa-standalone #pwaInstallBanner { display: none !important; }

/* Standalone mode: ให้ mobile menu ทำงานได้ (ถูกเปิดจาก bottom tab "เพิ่มเติม") */
@media (display-mode: standalone) {
    .mobile-menu { display: block !important; }
    .nav-desktop { display: none !important; }
    .nav-mobile-actions { display: flex !important; }
}
html.pwa-standalone .mobile-menu { display: block !important; }
html.pwa-standalone .nav-desktop { display: none !important; }
html.pwa-standalone .nav-mobile-actions { display: flex !important; }
