@import url('https://fonts.googleapis.com/css2?family=Sarabun:wght@300;400;500;600;700&family=Taviraj:wght@300;400;500;600;700&display=swap');

:root {
    /* Premium Color Palette */
    --sidebar-bg: #0f172a; /* Deep Slate */
    --sidebar-text: #e2e8f0;
    --sidebar-accent: #cda434; /* Elegant Gold */
    --sidebar-hover: rgba(255, 255, 255, 0.08);
    
    --app-bg: #f8fafc;
    --panel-bg: #ffffff;
    --book-bg: #fdfcf8;
    
    --text-main: #1e293b;
    --text-muted: #64748b;
    
    --primary: #cda434;
    --primary-dark: #b58d22;
    --primary-light: rgba(205, 164, 52, 0.1);
    --secondary: #334155;
    
    --border-light: #e2e8f0;
    --shadow-soft: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
    --shadow-hover: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --shadow-modal: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
}

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

body {
    background-color: var(--app-bg);
    color: var(--text-main);
    font-family: 'Sarabun', 'Noto Sans Thai', Tahoma, sans-serif;
    font-size: 16px;
    height: 100vh;
    overflow: hidden;
}

/* -------------------------------------
   App Shell & Nav
------------------------------------- */
.app-layout {
    display: flex;
    height: 100vh;
    width: 100vw;
}

.app-sidebar {
    width: 70px;
    background-color: var(--sidebar-bg);
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 1rem;
    z-index: 50;
    box-shadow: 4px 0 15px rgba(0,0,0,0.1);
}

.app-sidebar-logo i {
    font-size: 2rem;
    color: var(--sidebar-accent);
    margin-bottom: 2rem;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.3));
}

.app-main-nav {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    width: 100%;
    align-items: center;
}

.nav-btn {
    background: transparent;
    border: none;
    color: #64748b;
    font-size: 1.5rem;
    width: 48px;
    height: 48px;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    justify-content: center;
    align-items: center;
}

.nav-btn:hover:not(:disabled) {
    color: var(--sidebar-text);
    background-color: var(--sidebar-hover);
}

.nav-btn.active {
    color: var(--sidebar-accent);
    background-color: rgba(205, 164, 52, 0.15);
}

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

.app-main-content {
    flex-grow: 1;
    position: relative;
    overflow: hidden;
    background: var(--app-bg);
}

.app-screen {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

.app-screen.active {
    opacity: 1;
    visibility: visible;
}

/* -------------------------------------
   Screen 1: Project Home
------------------------------------- */
.screen-header {
    padding: 3rem 4rem 1rem;
}
.screen-header h1 {
    font-size: 2.5rem;
    font-weight: 700;
    color: var(--text-main);
    margin-bottom: 0.5rem;
}
.screen-header p {
    font-size: 1.1rem;
    color: var(--text-muted);
}

.projects-container {
    padding: 2rem 4rem;
}

.project-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1.5rem;
    margin-top: 1.5rem;
}

.project-card {
    background: var(--panel-bg);
    border-radius: 12px;
    padding: 2rem;
    box-shadow: var(--shadow-soft);
    cursor: pointer;
    transition: all 0.2s;
    border: 1px solid var(--border-light);
    display: flex;
    flex-direction: column;
}

.project-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-hover);
    border-color: var(--primary);
}

.project-card h3 {
    font-size: 1.4rem;
    margin-bottom: 0.5rem;
    color: var(--text-main);
}

.project-card p.type {
    font-size: 0.9rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 1rem;
}

.project-card .stats {
    margin-top: auto;
    font-size: 0.95rem;
    color: var(--secondary);
    display: flex;
    gap: 1rem;
}

.create-project {
    border: 2px dashed #cbd5e1;
    background: transparent;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    min-height: 180px;
}
.create-project i {
    font-size: 2rem;
    margin-bottom: 1rem;
}
.create-project:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: var(--primary-light);
}

/* -------------------------------------
   Screen 2: Dashboard
------------------------------------- */
.dashboard-grid {
    padding: 2rem 4rem;
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 2rem;
}

.dash-section {
    background: var(--panel-bg);
    border-radius: 12px;
    padding: 2rem;
    box-shadow: var(--shadow-soft);
    border: 1px solid var(--border-light);
}

.section-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    border-bottom: 1px solid var(--border-light);
    padding-bottom: 1rem;
}
.section-title h3 {
    font-size: 1.3rem;
    color: var(--secondary);
}

.primary-btn {
    background: var(--primary);
    color: white;
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    cursor: pointer;
    font-family: inherit;
    font-weight: 600;
    transition: background 0.2s;
}
.primary-btn:hover { background: var(--primary-dark); }

.dash-list {
    list-style: none;
}
.dash-list li {
    padding: 1rem;
    border-bottom: 1px solid #f1f5f9;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.dash-list li:last-child { border-bottom: none; }
.dash-list li.empty-state {
    color: var(--text-muted);
    font-style: italic;
    justify-content: center;
}
.law-item-title { font-weight: 600; font-size: 1.1rem; }
.law-item-type { font-size: 0.85rem; color: var(--text-muted); background: #f1f5f9; padding: 2px 8px; border-radius: 12px; }

.recent-card {
    margin-top: 1.5rem;
    padding: 1.5rem;
    background: var(--app-bg);
    border-radius: 8px;
    border: 1px solid var(--border-light);
}

/* -------------------------------------
   Screen 3: Law Reader (3 Panes)
------------------------------------- */
.reader-layout {
    flex-direction: row !important; /* override app-screen column */
    overflow: hidden !important;
}

.reader-toc {
    position: relative;
    width: 24px;
    min-width: 24px;
    background: var(--panel-bg);
    border-right: 1px solid var(--border-light);
    display: flex;
    flex-direction: column;
    z-index: 100;
    
    transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1), min-width 0.3s;
    box-shadow: none;
    overflow: hidden;
    flex-shrink: 0;
}

.reader-toc-inner {
    width: 250px;
    min-width: 250px;
    height: 100%;
    display: flex;
    flex-direction: column;
    opacity: 0;
    transition: opacity 0.2s;
    pointer-events: none;
}

.reader-toc::after {
    content: '';
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    width: 4px;
    height: 40px;
    background-color: #cbd5e1;
    border-radius: 4px;
    transition: opacity 0.2s;
    z-index: 10;
}

.reader-toc:hover,
.reader-toc:focus-within,
.reader-toc.force-open {
    width: 250px;
    min-width: 250px;
    box-shadow: 4px 0 15px rgba(0,0,0,0.03);
}

.reader-toc:hover .reader-toc-inner,
.reader-toc:focus-within .reader-toc-inner,
.reader-toc.force-open .reader-toc-inner {
    opacity: 1;
    pointer-events: auto;
}

.reader-toc:hover::after,
.reader-toc:focus-within::after,
.reader-toc.force-open::after {
    opacity: 0;
}
.toc-header {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border-light);
}
.toc-content {
    flex-grow: 1;
    overflow-y: auto;
    padding: 1rem 0;
}
.toc-law-group {
    margin-bottom: 1rem;
    transition: opacity 0.2s, border 0.2s;
}
.toc-law-group.dragging {
    opacity: 0.5;
}
.toc-law-group.drag-over-top {
    border-top: 3px solid var(--primary);
}
.toc-law-group.drag-over-bottom {
    border-bottom: 3px solid var(--primary);
}
.toc-law-title {
    padding: 0.8rem 1.2rem;
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--secondary);
    background: #f1f5f9;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    /* ปักหมุดชื่อกฎหมายไว้บนสุดของสารบัญขณะเลื่อน จะได้รู้ว่ากำลังอยู่กฎหมายใด */
    position: sticky;
    top: 0;
    z-index: 5;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
.toc-item {
    padding: 0.6rem 1.5rem 0.6rem 2.5rem;
    cursor: pointer;
    font-size: 1rem;
    transition: all 0.2s;
    border-left: 3px solid transparent;
}
.toc-item:hover { background: var(--app-bg); }
.toc-item.active {
    background: var(--primary-light);
    border-left-color: var(--primary);
    font-weight: 600;
    color: var(--primary-dark);
}

.toc-item-title {
    display: none;
}
.toc-item:hover .toc-item-title {
    display: block;
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 4px;
    font-weight: normal;
}

.reader-content-pane {
    flex-grow: 1;
    background: var(--book-bg);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    position: relative;
}
.reader-header {
    padding: 2rem 4rem 1rem;
    border-bottom: 1px solid var(--border-light);
    background: var(--book-bg);
    position: sticky;
    top: 0;
    z-index: 10;
}
.reader-header h2 {
    font-family: 'Sarabun', 'Noto Sans Thai', Tahoma, sans-serif;
    font-size: 18px;
    line-height: 1.45;
    color: var(--secondary);
}
.reader-text-area {
    padding: 3rem 4rem;
    font-size: 16px;
    line-height: 1.8;
    color: var(--text-main);
    font-family: 'Sarabun', 'Noto Sans Thai', Tahoma, sans-serif;
}
.reader-text-area p {
    margin-bottom: 1.5rem;
    text-indent: 0;
}

.reader-context-pane {
    position: relative;
    width: 24px;
    min-width: 24px;
    background: var(--panel-bg);
    border-left: 1px solid var(--border-light);
    display: flex;
    flex-direction: column;
    z-index: 100;
    
    transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1), min-width 0.3s;
    box-shadow: none;
    overflow: hidden;
    flex-shrink: 0;
}

.reader-context-pane-inner {
    width: 380px;
    min-width: 380px;
    height: 100%;
    display: flex;
    flex-direction: column;
    opacity: 0;
    transition: opacity 0.2s;
    pointer-events: none;
}

.reader-context-pane::before {
    content: '';
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    width: 4px;
    height: 40px;
    background-color: #cbd5e1;
    border-radius: 4px;
    transition: opacity 0.2s;
    z-index: 10;
}

.reader-context-pane:hover,
.reader-context-pane:focus-within,
.reader-context-pane.expanded {
    width: 380px;
    min-width: 380px;
    box-shadow: -4px 0 15px rgba(0,0,0,0.03);
}

.reader-context-pane:hover .reader-context-pane-inner,
.reader-context-pane:focus-within .reader-context-pane-inner,
.reader-context-pane.expanded .reader-context-pane-inner {
    opacity: 1;
    pointer-events: auto;
}

.reader-context-pane:hover::before,
.reader-context-pane:focus-within::before,
.reader-context-pane.expanded::before {
    opacity: 0;
}
/* Fullscreen mode: the context pane covers the whole viewport instead of
   sitting as a narrow side column. Its inner content stretches to fill. */
.reader-context-pane.fullscreen {
    position: fixed;
    inset: 0;
    width: 100vw;
    min-width: 100vw;
    height: 100vh;
    z-index: 5000;
    box-shadow: none;
    border-left: none;
}
.reader-context-pane.fullscreen .reader-context-pane-inner {
    width: 100%;
    min-width: 100%;
    opacity: 1;
    pointer-events: auto;
}
.reader-context-pane.fullscreen::before { opacity: 0; }

.context-tabs {
    display: flex;
    border-bottom: 1px solid var(--border-light);
    align-items: center;
}
/* Fullscreen toggle button living at the end of the tab bar. */
.ctx-fullscreen-btn {
    flex: 0 0 auto;
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--text-muted, #64748b);
    font-size: 1rem;
    padding: 0 14px;
    align-self: stretch;
}
.ctx-fullscreen-btn:hover { color: var(--primary, #0ea5e9); }
.tab-btn {
    flex: 1;
    background: transparent;
    border: none;
    padding: 1rem 0;
    font-family: inherit;
    font-weight: 600;
    color: var(--text-muted);
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: all 0.2s;
}
.tab-btn:hover { color: var(--secondary); }
.tab-btn.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}
.context-content {
    flex-grow: 1;
    overflow-y: auto;
    padding: 1.5rem;
}
.tab-pane {
    display: none;
    padding: 1.5rem;
}
.tab-pane.active {
    display: block;
    animation: fadeIn 0.3s ease;
}
#tab-erm.tab-pane.active {
    display: flex;
    flex-direction: column;
}
.empty-tab {
    color: var(--text-muted);
    text-align: center;
    margin-top: 2rem;
    font-style: italic;
}

/* Context Panel Cards */
.context-card {
    background: var(--app-bg);
    border: 1px solid var(--border-light);
    border-radius: 8px;
    padding: 1.2rem;
    margin-bottom: 1rem;
}
.context-card h4 {
    font-size: 1.1rem;
    color: var(--secondary);
    margin-bottom: 0.5rem;
}
.context-card p {
    font-size: 0.95rem;
    color: var(--text-main);
    line-height: 1.5;
}
.context-card .tag {
    display: inline-block;
    background: var(--primary-light);
    color: var(--primary-dark);
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

/* Legal Chain Stepper */
.chain-stepper {
    margin-top: 1rem;
}
.chain-step {
    display: flex;
    margin-bottom: 1rem;
    position: relative;
}
.chain-step:not(:last-child)::after {
    content: '';
    position: absolute;
    left: 11px;
    top: 24px;
    bottom: -16px;
    width: 2px;
    background: var(--border-light);
}
.step-dot {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--primary);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    margin-right: 1rem;
    flex-shrink: 0;
    z-index: 1;
}
.step-content h5 { font-size: 1rem; color: var(--secondary); margin-bottom: 0.2rem;}
.step-content p { font-size: 0.9rem; color: var(--text-muted); }

/* -------------------------------------
   Screen 4: Graph View
------------------------------------- */
.graph-layout {
    background: #fafafa;
}
.graph-header {
    padding: 2rem;
    background: white;
    border-bottom: 1px solid var(--border-light);
    box-shadow: var(--shadow-soft);
    z-index: 10;
}
.cy-container {
    flex-grow: 1;
    width: 100%;
}
.graph-detail-panel {
    position: absolute;
    bottom: 2rem;
    right: 2rem;
    width: 300px;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(5px);
    border-radius: 12px;
    padding: 1.5rem;
    box-shadow: var(--shadow-hover);
    border: 1px solid var(--border-light);
}
.graph-detail-panel h3 { font-size: 1.2rem; margin-bottom: 0.5rem; border-bottom: 1px solid var(--border-light); padding-bottom: 0.5rem;}
.graph-detail-panel p { font-size: 0.95rem; line-height: 1.5;}

/* -------------------------------------
   Modals
------------------------------------- */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; width: 100vw; height: 100vh;
    background: rgba(255, 255, 255, 0.6);
    backdrop-filter: blur(3px);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}
.modal-overlay.active { display: flex; animation: fadeIn 0.2s; }
.modal-content {
    background: var(--panel-bg);
    width: 90%;
    max-width: 500px;
    border-radius: 16px;
    box-shadow: var(--shadow-modal);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.modal-lg { max-width: 800px; height: 80vh; }
.modal-header {
    padding: 1.5rem 2rem;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.modal-header h2 { font-size: 1.5rem; color: var(--secondary); }
.close-modal { background: none; border: none; font-size: 1.5rem; color: var(--text-muted); cursor: pointer; }
.close-modal:hover { color: var(--text-main); }
.modal-body {
    padding: 2rem;
    flex-grow: 1;
    overflow-y: auto;
}
.form-group { margin-bottom: 1.5rem; }
.form-group label { display: block; font-weight: 600; margin-bottom: 0.5rem; color: var(--secondary); }
.form-group input, .form-group select {
    width: 100%; padding: 0.8rem; border: 1px solid var(--border-light); border-radius: 8px;
    font-family: inherit; font-size: 1rem;
}
.form-group input:focus, .form-group select:focus {
    outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light);
}
.modal-footer {
    padding: 1.5rem 2rem;
    border-top: 1px solid var(--border-light);
    display: flex;
    justify-content: flex-end;
    gap: 1rem;
}
.btn-cancel {
    background: transparent; border: 1px solid var(--border-light); padding: 0.5rem 1rem; border-radius: 6px; cursor: pointer;
}
.btn-primary {
    background: var(--primary); color: white; border: none; padding: 0.5rem 1.5rem; border-radius: 6px; cursor: pointer; font-weight: 600;
}

/* Law Library List in Modal */
.law-list { margin-top: 1rem; }
.library-item {
    display: flex; justify-content: space-between; align-items: center;
    padding: 1rem; border: 1px solid var(--border-light); border-radius: 8px; margin-bottom: 0.5rem;
    transition: all 0.2s;
}
.library-item:hover { border-color: var(--primary); background: #fafafa; }
.library-item-info strong { display: block; font-size: 1.1rem; }
.library-item-info span { font-size: 0.85rem; color: var(--text-muted); }

/* -------------------------------------
   Active Recall Toolbar & Highlighting
------------------------------------- */
.user-highlight {
    background-color: #fef08a;
    border-radius: 3px;
    padding: 0 4px;
    color: #000;
}
.user-blur {
    background-color: #ffffff;
    color: #ffffff;
    box-shadow: inset 0 0 0 1px #e2e8f0;
    border-radius: 4px;
    padding: 0 4px;
    cursor: pointer;
    transition: background-color 0.2s, color 0.2s;
}
.user-blur:hover { background-color: transparent; color: #1e293b; box-shadow: none; border: 1px dashed #94a3b8; }

/* -------------------------------------
   Collapse mode: remove redacted words entirely so the
   remaining (un-redacted) text reads together, condensed.
------------------------------------- */
.reader-content-pane.mode-collapse .user-blur {
    display: none;
}
/* When collapsing, kill the leading/trailing whitespace the removed
   span leaves behind so words butt up cleanly. */
.reader-content-pane.mode-collapse .block-body {
    letter-spacing: normal;
}

/* -------------------------------------
   Principle mode: show only the key legal principle per block,
   editable & saved. Sources: user-redaction / Claude-extracted.
------------------------------------- */
.reader-content-pane.mode-principle .block-body {
    display: none;
}
.principle-panel {
    display: none;
    margin-top: 8px;
    border-left: 3px solid var(--primary, #4f46e5);
    background: #f8fafc;
    border-radius: 8px;
    padding: 10px 14px;
}
.reader-content-pane.mode-principle .principle-panel {
    display: block;
}
.principle-text {
    font-size: 1rem;
    line-height: 1.75;
    color: #0f172a;
    min-height: 1.75em;
    outline: none;
    white-space: pre-wrap;
}
.principle-text:empty::before {
    content: attr(data-placeholder);
    color: #94a3b8;
}
.principle-text[contenteditable="true"]:focus {
    background: #fff;
    box-shadow: inset 0 0 0 1px var(--primary, #4f46e5);
    border-radius: 4px;
    padding: 4px 6px;
}

/* -------------------------------------
   Memorize / exam mode (โหมดท่องจำ = อ่านสอบ):
   random cloze blanks — คำถูกซ่อนเป็น ______ คลิกเพื่อดูเฉลยทีละช่อง
------------------------------------- */
/* คำจริงอยู่ใน textContent แต่โปร่งใส → ช่องกว้างเท่าคำเป๊ะ, layout นิ่งตอนเฉลย
   (ไม่ใช้ _ monospace อีกต่อไป — ความกว้างจึงตรงกับคำที่ต้องทายจริง) */
.cloze-blank {
    color: transparent;
    background: #eef2ff;
    border-bottom: 2px solid #6366f1;
    border-radius: 3px;
    padding: 0 2px;
    cursor: pointer;
    user-select: none; /* กันลากเผยคำจริง (transparent text) โดยไม่ตั้งใจ */
    -webkit-user-select: none;
    transition: background-color 0.15s, color 0.15s;
}
.cloze-blank:hover {
    background: #dbe0ff;
}
.cloze-blank.revealed {
    color: #4338ca; /* เผยคำจริง — textContent เดิม แค่เปลี่ยนสี ไม่ขยับ layout */
    background: #e0e7ff;
    font-weight: 600;
}

/* แถบตัวเลือกโหมดท่องจำ (ระดับความยาก + สุ่มใหม่) — โผล่เฉพาะตอนโหมดท่องจำ */
.cloze-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    align-items: center;
    width: 100%;
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px dashed #e2e8f0;
}
/* ซ่อนแถบนี้ทุกโหมดยกเว้นท่องจำ */
.reading-mode-toggle .cloze-controls { display: none; }
.reader-content-pane.mode-memorize .reading-mode-toggle .cloze-controls { display: flex; }
.cloze-controls-label {
    font-size: 0.85rem;
    color: #64748b;
    font-weight: 600;
}
.cloze-level-btn,
.cloze-shuffle-btn {
    font-size: 0.85rem;
    padding: 3px 10px;
    border: 1px solid #cbd5e1;
    background: #fff;
    color: #334155;
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.15s, color 0.15s, border-color 0.15s;
}
.cloze-level-btn:hover,
.cloze-shuffle-btn:hover {
    background: #f1f5f9;
    border-color: #94a3b8;
}
.cloze-level-btn.active {
    background: var(--primary, #4f46e5);
    color: #fff;
    border-color: var(--primary, #4f46e5);
}
.cloze-shuffle-btn {
    margin-left: auto;
    color: var(--primary, #4f46e5);
    border-color: #c7d2fe;
}
.cloze-shuffle-btn:hover {
    background: #eef2ff;
}

.selection-toolbar {
    position: absolute;
    background: transparent;
    border: none;
    padding: 0;
    display: none;
    box-shadow: none;
    z-index: 2000;
    gap: 6px;
    transform: translate(-50%, -120%);
    animation: popIn 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.tool-btn {
    background: transparent; 
    border: none; 
    padding: 4px; 
    border-radius: 50%; 
    cursor: pointer; 
    font-size: 0.9rem; 
    display: flex; 
    align-items: center; 
    justify-content: center;
    width: 28px;
    height: 28px;
    opacity: 0.6;
    color: #94a3b8;
    text-shadow: 0 1px 2px rgba(0,0,0,0.1);
    transition: transform 0.15s, background 0.15s, opacity 0.15s;
}
.tool-btn:hover { 
    background: rgba(255,255,255,0.9); 
    opacity: 1;
    transform: scale(1.15);
}
.highlight-btn i { color: #fde047; } /* Pastel Yellow */
.blur-btn i { color: #cbd5e1; } /* Pastel Slate */
.note-btn i { color: #93c5fd; } /* Pastel Blue */
.clear-btn i { color: #fca5a5; } /* Pastel Red */

@keyframes popIn {
    from { opacity: 0; transform: translate(-50%, -100%) scale(0.9); }
    to { opacity: 1; transform: translate(-50%, -120%) scale(1); }
}
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* -------------------------------------
   Full Law Reader Blocks
------------------------------------- */
.toc-empty {
    padding: 1rem;
    color: var(--text-muted);
    text-align: center;
}

.toc-law-title-btn {
    width: 100%;
    border: none;
    text-align: left;
    cursor: pointer;
    font-family: inherit;
    line-height: 1.35;
}

.toc-heading {
    padding-left: 1.4rem;
    font-weight: 600;
    color: var(--text-muted);
    background: #ffffff;
    font-size: 0.95rem;
    border-bottom: 1px dashed var(--border-light);
    display: flex;
    align-items: center;
}

.toc-law-content {
    transition: max-height 0.3s ease;
}
.toc-law-content.collapsed {
    display: none;
}

.toc-heading-content {
    transition: max-height 0.3s ease;
}
.toc-heading-content.collapsed {
    display: none;
}

.toc-heading-btn {
    width: 100%;
    border: none;
    text-align: left;
    cursor: pointer;
    font-family: inherit;
}
.toc-heading-btn:hover {
    background: var(--app-bg);
}

.reader-text-area {
    max-width: 1200px;
    width: 100%;
    margin: 0 auto;
}

.reader-block {
    scroll-margin-top: 118px;
    margin-bottom: 0.9rem;
    border-left: 3px solid transparent;
}

/* -------------------------------------
   Vision memory (B1): สันสีซ้ายประจำกฎหมาย + ไล่เฉดเข้มขึ้นเล็กน้อยตามลำดับหมวด
   --law-hue มาจาก inline style ต่อ block; พื้น block ยังขาวเสมอ (สีอยู่แค่ขอบ/label)
   lightness เริ่ม 72% แล้วลดทีละ 3% ต่อหมวด (คุมไม่ต่ำกว่า ~57% = ยังพาสเทล)
------------------------------------- */
.reader-block[data-law-id] {
    border-left-color: hsl(var(--law-hue, 220), 45%, calc(72% - var(--muad-step, 0) * 3%));
}
.reader-block[data-muad-idx="0"] { --muad-step: 0; }
.reader-block[data-muad-idx="1"] { --muad-step: 0; }
.reader-block[data-muad-idx="2"] { --muad-step: 1; }
.reader-block[data-muad-idx="3"] { --muad-step: 2; }
.reader-block[data-muad-idx="4"] { --muad-step: 3; }
.reader-block[data-muad-idx="5"] { --muad-step: 4; }
.reader-block[data-muad-idx="6"] { --muad-step: 5; }

.block-shell {
    background: #ffffff; /* พื้น block ขาวล้วน (constraint จากผู้ใช้ — สีไปอยู่ที่ขอบ/label เท่านั้น) */
    border: 1px solid rgba(226, 232, 240, 0.9);
    border-radius: 8px;
    padding: 1rem 1.2rem 1.2rem;
    transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
}

/* ไอคอนประจำกฎหมาย (B3) หน้าเลขมาตรา — สี = เฉดเข้มของ theme, เล็ก ไม่เด่นเกิน */
.block-law-icon {
    color: hsl(var(--law-hue, 220), 42%, 52%);
    font-size: 0.95rem;
    flex: 0 0 auto;
    opacity: 0.85;
}
/* เลขมาตราใช้สีเฉดเข้มของ theme (ยังอ่านชัด ไม่ใช่สีรบกวน) */
.reader-block[data-law-id] .block-label {
    color: hsl(var(--law-hue, 220), 35%, 38%);
}

/* เส้นคั่นหมวด (B4 visual chunking) — เส้นบางธรรมดา + ชื่อหมวด ไม่ sticky */
.muad-divider {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin: 1.4rem 0 0.8rem;
    border-top: 1px solid hsl(var(--law-hue, 220), 30%, 80%);
    padding-top: 0.5rem;
}
.muad-divider-label {
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    color: hsl(var(--law-hue, 220), 30%, 45%);
}

.reader-block.selected .block-shell {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.reader-block.is-highlighted .block-shell {
    background: #fff8db;
    border-color: #f2d06b;
}

.reader-block.is-ticked .block-label::before {
    content: '\f00c';
    font-family: 'Font Awesome 6 Free';
    font-weight: 900;
    color: #16a34a;
    margin-right: 0.45rem;
}

/* -------------------------------------
   Capture Mode (โหมดถ่ายภาพ)
   ดัน block ที่ selected ให้นูนคมชัดเหมือน subject ของภาพ
   เบลอ+หรี่+ย่อ block อื่นทั้งหมดเป็นฉากหลัง (depth-of-field)
   ปุ่ม .capture-btn = toggle อิสระ ไม่ทับ mode อ่าน
------------------------------------- */
.reader-content-pane.capture-active .reader-text-area {
    background: rgba(15, 23, 42, 0.045); /* ฉากหลังหรี่ลงนิด ให้ subject เด่น */
    transition: background 0.25s ease;
}

/* block ทั่วไป = ฉากหลัง: เบลอ จาง ย่อ กดลง */
.reader-content-pane.capture-active .reader-block {
    filter: blur(3px);
    opacity: 0.4;
    transform: scale(0.97);
    transform-origin: center;
    transition: filter 0.28s ease, opacity 0.28s ease, transform 0.28s ease;
    pointer-events: auto; /* ยังคลิกเพื่อย้ายโฟกัสได้ */
}

/* block ที่โฟกัส = subject: นูนขึ้นมา คมชัด สว่างเต็ม เงาลึก */
.reader-content-pane.capture-active .reader-block.selected {
    filter: none;
    opacity: 1;
    transform: scale(1.035);
    position: relative;
    z-index: 5;
}
.reader-content-pane.capture-active .reader-block.selected .block-shell {
    background: #ffffff;
    border-color: var(--primary);
    box-shadow:
        0 0 0 3px var(--primary-light),
        0 18px 45px -12px rgba(15, 23, 42, 0.45),
        0 8px 18px -8px rgba(15, 23, 42, 0.3);
}

/* ปุ่มถ่ายภาพตอน active — ให้รู้ว่ากำลังเปิดอยู่ */
.mode-btn.capture-btn.active {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}

@media (prefers-reduced-motion: reduce) {
    .reader-content-pane.capture-active .reader-block,
    .reader-content-pane.capture-active .reader-block.selected {
        transition: none;
    }
}

.block-topline {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.8rem;
    min-height: 36px;
    margin-bottom: 0.6rem;
}

.block-title-btn {
    min-width: 0;
    flex-shrink: 0;
    border: 0;
    background: transparent;
    padding: 0;
    cursor: pointer;
    text-align: left;
    font-family: inherit;
    color: var(--secondary);
    display: flex;
    align-items: baseline;
    gap: 0.6rem;
}

.block-label {
    font-weight: 700;
    font-size: 18px;
    line-height: 1.45;
}

/* โหมดแก้ไขตัวบท: กล่อง contenteditable ให้เห็นชัดว่ากำลังแก้ได้ */
.block-body.is-editing {
    outline: 2px dashed var(--primary, #cda434);
    outline-offset: 4px;
    border-radius: 6px;
    background: #fffdf5;
    padding: 8px 10px;
    white-space: pre-wrap; /* คงการเยื้อง/เว้นบรรทัดขณะแก้ */
    min-height: 2em;
}
.block-edit-hint {
    margin-top: 8px;
    font-size: 0.8rem;
    color: var(--text-muted);
    background: #fff7e6;
    border: 1px solid #f5e2b8;
    border-radius: 6px;
    padding: 6px 10px;
}

.block-type {
    font-size: 0.72rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.block-meta-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.6rem;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.block-breadcrumb {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 500;
}

.section-status-bar {
    display: flex;
    gap: 0.5rem;
    font-size: 0.75rem;
    font-weight: 600;
}

.status-badge {
    padding: 0.15rem 0.5rem;
    border-radius: 4px;
    background: #f1f5f9;
    color: var(--text-muted);
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
}

.status-badge.review {
    background: #fff1f2;
    color: #e11d48;
}

.status-badge.memorized {
    background: #f0fdf4;
    color: #16a34a;
}

.block-actions-wrapper {
    position: relative;
    display: inline-block;
}

.block-actions.ribbon-menu {
    display: flex;
    white-space: nowrap;
    gap: 2px;
}

.block-actions button {
    width: 24px;
    height: 24px;
    border: none;
    background: transparent;
    color: #94a3b8; /* Pastel default */
    border-radius: 4px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    opacity: 0.6;
    transition: background 0.15s, color 0.15s, opacity 0.15s;
}

.block-actions button:hover {
    color: var(--primary);
    background: rgba(255,255,255,0.9);
    opacity: 1;
}

.block-actions button.active {
    color: var(--primary);
    background: var(--primary-light);
}

.block-body {
    font-family: 'Sarabun', 'Noto Sans Thai', Tahoma, sans-serif;
    font-size: 16px;
    line-height: 1.8;
}

.case-indicator-btn:hover {
    transform: scale(1.1);
    opacity: 1 !important;
}

.reader-text-area .law-paragraph {
    margin: 0 0 0.7rem;
    text-indent: 2.5em;
}

.law-subitem {
    display: grid;
    grid-template-columns: 38px minmax(0, 1fr);
    column-gap: 0.45rem;
    margin: 0.45rem 0 0.45rem 2.5em; /* ขยับให้เท่ากับ text-indent ของย่อหน้า */
}

.law-subitem.level-2 {
    margin-left: 5em; /* ขยับเข้าไปอีกสำหรับระดับ 2 */
}

.law-marker {
    color: var(--text-muted);
    font-weight: 600;
    text-align: right;
}

.block-heading .block-body {
    font-family: 'Sarabun', 'Noto Sans Thai', Tahoma, sans-serif;
    font-size: 18px;
    color: var(--secondary);
}

.block-heading .block-body .law-paragraph {
    text-indent: 0;
    font-weight: 600;
}

.block-note {
    font-family: 'Sarabun', sans-serif;
    font-size: 0.95rem;
    line-height: 1.45;
    color: #713f12;
    background: #fef3c7;
    border: 1px solid #fde68a;
    border-radius: 6px;
    padding: 0.55rem 0.7rem;
    margin-bottom: 0.75rem;
}

.block-hidden-label {
    display: none;
    color: var(--text-muted);
    font-family: 'Sarabun', sans-serif;
    font-size: 0.92rem;
    padding: 0.75rem 0;
}

.reader-block.is-hidden {
    display: none !important;
}

.reader-block.is-blurred .block-body {
    filter: blur(5px);
    transition: filter 0.15s;
}

.reader-block.is-blurred:hover .block-body,
.reader-block.is-blurred.selected .block-body {
    filter: none;
}

.law-link {
    color: var(--primary);
    text-decoration: none;
    border-bottom: 1px dashed var(--primary);
    transition: all 0.15s;
    cursor: pointer;
}
.law-link:hover {
    color: var(--primary-dark);
    border-bottom-style: solid;
    background: var(--primary-light);
}

.selected-block-card {
    border-color: var(--primary);
}
.case-card h4 {
    font-size: 1rem;
    line-height: 1.45;
}

.case-panel-summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.85rem 1rem;
    margin-bottom: 1rem;
    border: 1px solid var(--border-light);
    border-radius: 8px;
    background: #fff;
}

.case-panel-summary strong {
    font-size: 1.45rem;
    color: var(--primary-dark);
    margin-right: 0.4rem;
}

.case-panel-summary span {
    color: var(--text-muted);
    font-size: 0.88rem;
}

.case-group {
    border: 1px solid var(--border-light);
    border-radius: 8px;
    background: #fff;
    margin-bottom: 0.8rem;
    overflow: hidden;
}

.case-group summary {
    list-style: none;
    cursor: pointer;
    padding: 0.8rem 0.95rem;
    display: flex;
    justify-content: space-between;
    gap: 0.8rem;
    align-items: center;
    color: var(--secondary);
    font-weight: 700;
    line-height: 1.45;
}

.case-group summary::-webkit-details-marker {
    display: none;
}

.case-group summary strong {
    flex-shrink: 0;
    min-width: 32px;
    text-align: center;
    color: var(--primary-dark);
    background: var(--primary-light);
    border-radius: 999px;
    padding: 0.15rem 0.45rem;
    font-size: 0.82rem;
}

.case-group .case-card {
    border-left: 0;
    border-right: 0;
    border-radius: 0;
    margin: 0;
}

.case-group .case-card + .case-card {
    border-top: 0;
}

.case-card.is-extra,
.case-group.is-extra-group {
    display: none;
}

.case-card-topline {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin-bottom: 0.55rem;
}

.case-card-topline .tag {
    margin-bottom: 0;
}

.confidence-badge,
.case-count-badge {
    display: inline-flex;
    align-items: center;
    min-height: 22px;
    padding: 0.1rem 0.45rem;
    border-radius: 999px;
    font-size: 0.74rem;
    font-weight: 700;
}

.confidence-high {
    background: #dcfce7;
    color: #166534;
}

.confidence-medium {
    background: #fef3c7;
    color: #92400e;
}

.confidence-low,
.confidence-unknown {
    background: #fee2e2;
    color: #991b1b;
}

.case-count-badge {
    background: #e0f2fe;
    color: #075985;
}

.case-show-more {
    width: 100%;
    border: 0;
    border-top: 1px solid var(--border-light);
    background: #f8fafc;
    color: var(--primary-dark);
    cursor: pointer;
    font-family: inherit;
    font-weight: 700;
    padding: 0.75rem;
}

.case-show-more:hover {
    background: var(--primary-light);
}
.case-meta,
.case-more {
    margin-top: 0.75rem;
    color: var(--text-muted);
    font-size: 0.82rem;
}

/* --- New UI Components for AntiGravity Spec --- */

/* TOC Actions & Heatmap */
.toc-item {
    position: relative;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 1rem;
}
.toc-item-label {
    flex-grow: 1;
}
.toc-item-actions {
    display: none;
    gap: 4px;
}
.toc-item:hover .toc-item-actions {
    display: flex;
}
.toc-item-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 4px;
}
.toc-item-btn:hover {
    color: var(--secondary);
    background: var(--border-light);
}
.toc-item.is-ticked {
    color: #16a34a;
}
.heatmap-dot {
    width: 6px; height: 6px; border-radius: 50%; display: inline-block; margin-left: 4px;
}
.dot-case { background-color: #cda434; }
.dot-note { background-color: #3b82f6; }
.dot-highlight { background-color: #fef08a; }
.dot-blur { background-color: #94a3b8; }
.dot-review { background-color: #ef4444; }

/* Hidden Drawer */
.toc-hidden-drawer-container {
    background: #f8fafc;
    border-bottom: 1px solid var(--border-light);
}
.hidden-drawer-btn {
    width: 100%;
    padding: 0.75rem 1rem;
    background: transparent;
    border: none;
    text-align: left;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.hidden-drawer-btn:hover { background: #f1f5f9; }
.hidden-drawer-content {
    padding: 0 1rem 1rem;
    max-height: 40vh;
    overflow-y: auto;
}
.hidden-drawer-content h5 {
    /* ปักหมุดชื่อกฎหมายไว้บนสุดขณะเลื่อนรายการที่ซ่อน เหมือนสารบัญหลัก */
    position: sticky;
    top: 0;
    margin: 0;
    padding: 0.5rem 0;
    background: #f8fafc;
    z-index: 1;
}
.hidden-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.9rem;
    padding: 0.4rem 0;
    border-bottom: 1px dashed var(--border-light);
}
.btn-restore-hidden {
    background: transparent;
    border: 1px solid var(--border-light);
    border-radius: 4px;
    padding: 2px 6px;
    font-size: 0.8rem;
    cursor: pointer;
}
.btn-restore-hidden:hover { background: #fff; }

/* Section Workspace / Status Bar */
.section-status-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 0.8rem;
    font-size: 0.85rem;
}
.status-badge {
    background: #f1f5f9;
    padding: 2px 8px;
    border-radius: 12px;
    color: var(--secondary);
    display: flex;
    align-items: center;
    gap: 4px;
}
.status-badge.review { background: #fee2e2; color: #991b1b; }
.status-badge.memorized { background: #dcfce7; color: #166534; }
.block-breadcrumb {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 0.4rem;
}

/* Compare Pane */
.compare-pane-container {
    background: #f8fafc;
    border-bottom: 1px solid var(--border-light);
    box-shadow: inset 0 -4px 6px -4px rgba(0,0,0,0.05);
    padding: 1rem 4rem;
    max-height: 40vh;
    overflow-y: auto;
}
.compare-pane-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-light);
}
.close-compare-btn {
    background: transparent; border: none; cursor: pointer; color: var(--text-muted);
}
.close-compare-btn:hover { color: var(--secondary); }

/* Reading Mode Toggle */
.reader-header-top { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 0; position: relative; }

/* ปุ่มเรียกโหมดการอ่าน — ขนาดเล็ก สีจาง ไม่ดึงสายตาไปจากตัวบท
   แถบเลือกโหมดจริงถูกซ่อนไว้ (dropdown) จนกว่าจะคลิกปุ่มนี้ */
.reading-mode-fab {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: transparent;
    border: 1px solid var(--border-light);
    color: var(--text-muted);
    padding: 4px 12px;
    border-radius: 999px;
    font-family: inherit;
    font-size: 0.82rem;
    cursor: pointer;
    transition: all 0.15s;
}
.reading-mode-fab:hover,
.reading-mode-fab[aria-expanded="true"] {
    background: var(--border-light);
    color: var(--secondary);
}

.reading-mode-toggle {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    background: #fff;
    padding: 4px;
    border-radius: 8px;
    border: 1px solid var(--border-light);
    box-shadow: 0 6px 20px rgba(0,0,0,0.12);
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    z-index: 20;
}
.reading-mode-toggle[hidden] { display: none; }
.mode-btn {
    background: transparent;
    border: none;
    padding: 4px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.9rem;
    color: var(--text-muted);
}
.mode-btn.active {
    background: #fff;
    color: var(--secondary);
    font-weight: 600;
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

/* Review List */
.review-container { padding: 2rem 4rem; }
.review-list {
    display: grid;
    gap: 1rem;
}
.review-item {
    background: #fff;
    border: 1px solid var(--border-light);
    border-radius: 8px;
    padding: 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.review-item h4 { margin-bottom: 0.25rem; font-size: 1.1rem; }
.review-item p { font-size: 0.9rem; color: var(--text-muted); }

/* Quick Action Menu */
.block-actions {
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.2s ease;
}
.reader-block:hover .block-actions,
.reader-block.selected .block-actions {
    opacity: 1;
}
.inline-action-btn {
    width: 32px;
    height: 32px;
    border: 1px solid transparent;
    background: transparent;
    color: var(--text-muted);
    border-radius: 6px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}
.inline-action-btn:hover {
    background: #f1f5f9;
    color: var(--secondary);
}
.inline-action-btn.active {
    color: var(--primary);
    background: var(--primary-light);
}
.case-more {
    text-align: center;
}

@media (max-width: 960px) {
    .reader-toc,
    .reader-context-pane {
        width: 24px;
        min-width: 24px;
    }
    .reader-context-pane:hover,
    .reader-context-pane:focus-within {
        width: 280px;
        min-width: 280px;
    }
    .reader-text-area {
        padding: 2rem 1.5rem;
    }
    .block-topline {
        align-items: stretch;
        flex-direction: column;
    }
    .block-topline > div {
        align-self: flex-end;
    }
}

/* Global Audio Player */
.audio-player-bar {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    width: 90%;
    max-width: 600px;
    background: rgba(30, 41, 59, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 999px;
    padding: 10px 20px;
    display: flex;
    justify-content: center;
    box-shadow: 0 10px 25px rgba(0,0,0,0.3);
    z-index: 9999;
    color: white;
    transition: bottom 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275), opacity 0.3s;
    font-family: 'Sarabun', sans-serif;
}

.audio-player-bar.hidden {
    bottom: -100px;
    opacity: 0;
    pointer-events: none;
}

.player-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    gap: 15px;
}

.player-info {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
}

.player-title {
    font-weight: 600;
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.player-status {
    font-size: 0.75rem;
    color: #94a3b8;
}

.player-controls {
    display: flex;
    align-items: center;
    gap: 8px;
}

.player-options {
    display: flex;
    align-items: center;
    gap: 8px;
}

.player-btn {
    background: transparent;
    border: none;
    color: #e2e8f0;
    cursor: pointer;
    font-size: 1.1rem;
    padding: 6px;
    border-radius: 50%;
    transition: background 0.2s, color 0.2s, transform 0.1s;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
}

.player-btn:hover {
    background: rgba(255,255,255,0.1);
    color: white;
}

.player-btn:active {
    transform: scale(0.9);
}

.play-btn-main {
    background: #fef08a;
    color: #854d0e;
    width: 42px;
    height: 42px;
    font-size: 1.2rem;
}

.play-btn-main:hover {
    background: #fde047;
    color: #713f12;
}

.player-select {
    background: rgba(255,255,255,0.1);
    color: white;
    border: none;
    border-radius: 4px;
    padding: 4px 6px;
    font-size: 0.8rem;
    cursor: pointer;
    outline: none;
    font-family: inherit;
}
.player-select option {
    background: #1e293b;
    color: white;
}

.player-btn.loop-all { color: #3b82f6; }
.player-btn.loop-one { color: #10b981; }
.player-btn.loop-none { color: #64748b; }

/* Reader section title metadata */
.block-title-stack {
    display: flex;
    min-width: 0;
    flex: 1;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 12px; /* เพิ่มระยะห่างให้ชื่อมาตราไม่ติดกับเลขมาตรา */
}

/* ชื่อตัวบท: คลิกที่ว่างข้างเลขมาตราแล้วพิมพ์แก้ได้ทันที (inline edit เหมือน block) */
.block-section-title {
    flex: 1;
    min-width: 40px;
    max-width: 100%;
    font-size: 1.05rem;
    line-height: 1.4;
    overflow-wrap: anywhere;
    color: #0284c7; /* สีฟ้าเข้มให้ต่างจากเลขมาตราและตัวบท */
    font-weight: 600;
    cursor: text;
    outline: none;
    border-radius: 4px;
    padding: 1px 4px;
    margin: -1px -4px;
}
.block-section-title:focus {
    background: #fff;
    box-shadow: inset 0 0 0 1px #0284c7;
}

.law-anchor-title {
    display: none !important;
}

.law-anchor-title.is-draft {
    display: none !important;
}

.toc-item-label {
    min-width: 0;
}

.toc-item-main,
.toc-item-title {
    display: block;
}

.toc-item-title {
    margin-top: 0.1rem;
    color: var(--text-muted);
    font-size: 0.78rem;
    font-weight: 500;
    line-height: 1.25;
}

/* ERM Feature */

.erm-header {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--bg-color);
}

.erm-header h3 {
    margin: 0;
    font-size: 1.1rem;
    color: var(--text-dark);
}

.erm-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    background: #fafafa;
}

.erm-node {
    background: #fff;
    border: 2px solid var(--primary-light);
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 16px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    position: relative;
    cursor: pointer;
    transition: all 0.2s;
}

.erm-node:hover {
    border-color: var(--primary);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.erm-node-type {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--primary);
    margin-bottom: 4px;
}

.erm-node-text {
    font-size: 0.95rem;
    color: var(--text-dark);
}

.erm-edge {
    text-align: center;
    color: var(--text-muted);
    font-size: 0.85rem;
    margin: -8px 0 8px 0;
    position: relative;
}

.erm-edge::before {
    content: '';
    position: absolute;
    left: 50%;
    top: -8px;
    height: 8px;
    width: 2px;
    background: var(--primary-light);
}

.erm-edge::after {
    content: '▼';
    color: var(--primary-light);
    display: block;
    font-size: 12px;
    margin-top: -2px;
}

/* User highlights for ERM */
.erm-highlight {
    background: rgba(14, 165, 233, 0.15);
    border-bottom: 2px dashed var(--primary);
    border-radius: 2px;
    cursor: pointer;
    position: relative;
}
.erm-highlight:hover {
    background: rgba(14, 165, 233, 0.3);
}

.erm-linking-source {
    animation: ermPulse 1.5s infinite;
}

@keyframes ermPulse {
    0% { box-shadow: 0 0 0 0 rgba(14, 165, 233, 0.4); }
    70% { box-shadow: 0 0 0 6px rgba(14, 165, 233, 0); }
    100% { box-shadow: 0 0 0 0 rgba(14, 165, 233, 0); }
}

/* ERM Color Templates */
.erm-node.node-condition {
    border-color: #f97316; /* Orange */
}
.erm-node.node-condition .erm-node-type {
    color: #f97316;
}

.erm-node.node-consequence {
    border-color: #0284c7; /* Blue */
}
.erm-node.node-consequence .erm-node-type {
    color: #0284c7;
}

/* Flashcard Mode */
#tab-erm.flashcard-mode .erm-node.node-target:not(.revealed) {
    background: #f1f5f9;
    border-style: dashed;
}
#tab-erm.flashcard-mode .erm-node.node-target:not(.revealed) .erm-node-text {
    visibility: hidden;
    position: relative;
}
#tab-erm.flashcard-mode .erm-node.node-target:not(.revealed) .erm-node-text::after {
    content: '?';
    visibility: visible;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 1.5rem;
    color: var(--text-muted);
    font-weight: 700;
}
.erm-node.node-target.revealed {
    animation: flashcardReveal 0.3s ease-out;
}
@keyframes flashcardReveal {
    0% { transform: scale(0.95); opacity: 0.5; }
    100% { transform: scale(1); opacity: 1; }
}

/* Toggle Switch */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
}
.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.toggle-switch .slider {
  position: absolute;
  cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background-color: #ccc;
  transition: .4s;
}
.toggle-switch .slider:before {
  position: absolute;
  content: "";
  height: 14px;
  width: 14px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: .4s;
}
.toggle-switch input:checked + .slider {
  background-color: var(--primary);
}
.toggle-switch input:checked + .slider:before {
  transform: translateX(16px);
}
.toggle-switch .slider.round {
  border-radius: 20px;
}
.toggle-switch .slider.round:before {
  border-radius: 50%;
}

/* ERM Relation Popover (Premium UI) */
.erm-relation-popover {
    position: fixed;
    z-index: 10000;
    background: rgba(255, 255, 255, 0.98);
    border: 1px solid rgba(15, 23, 42, 0.08);
    border-radius: 18px;
    box-shadow: 0 20px 45px -12px rgba(15,23,42,0.28), 0 8px 16px -8px rgba(15,23,42,0.18);
    padding: 14px;
    width: 264px;
    flex-direction: column;
    gap: 10px;
    backdrop-filter: blur(16px) saturate(160%);
    -webkit-backdrop-filter: blur(16px) saturate(160%);
    animation: ermPopIn 0.16s cubic-bezier(0.16, 1, 0.3, 1);
    transform-origin: top left;
}
@keyframes ermPopIn {
    from { opacity: 0; transform: scale(0.94) translateY(-4px); }
    to   { opacity: 1; transform: scale(1) translateY(0); }
}

.erm-relation-header {
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    color: #64748b;
    text-transform: none;
    padding: 2px 4px 6px;
    border-bottom: 1px solid rgba(15,23,42,0.06);
    text-align: left;
}

.erm-relation-options {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.erm-relation-btn {
    display: flex;
    align-items: center;
    gap: 12px;
    background: #f8fafc;
    border: 1.5px solid transparent;
    padding: 11px 14px;
    border-radius: 12px;
    cursor: pointer;
    transition: transform 0.15s cubic-bezier(0.16, 1, 0.3, 1), background 0.15s, border-color 0.15s, box-shadow 0.15s;
    width: 100%;
    text-align: left;
    font-family: inherit;
}
.erm-relation-btn:hover {
    background: #fff;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px -4px rgba(15,23,42,0.18);
}
.erm-relation-btn:active { transform: translateY(0); }
/* Color-coded accent per relation on hover */
.erm-relation-btn[data-relation="cause_effect"]:hover { border-color: #f97316; }
.erm-relation-btn[data-relation="rule_exception"]:hover { border-color: #3b82f6; }
.erm-relation-btn[data-relation="parent_child"]:hover { border-color: #10b981; }

/* ERM Marker Classes */
.erm-node-marker {
    border-radius: 4px;
    padding: 0 4px;
    background: transparent;
    transition: background 0.2s, border 0.2s;
    border: 2px solid transparent;
}
.erm-node-marker:hover {
    background: rgba(0,0,0,0.05);
}
body.erm-mode-active .erm-marker-cause {
    border: 2px solid #f97316; /* Orange */
}
body.erm-mode-active .erm-marker-rule {
    border: 2px solid #3b82f6; /* Blue */
}
body.erm-mode-active .erm-marker-major {
    border: 2px solid #10b981; /* Green */
}

/* ERM linking (waiting for target selection) */
body.erm-linking,
body.erm-linking .reader-content {
    cursor: crosshair;
}
body.erm-linking::after {
    content: "ลากคลุมข้อความปลายทางแล้วปล่อยเมาส์เพื่อเชื่อม  •  กด Esc เพื่อยกเลิก";
    position: fixed;
    top: 12px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(30, 41, 59, 0.95);
    color: #fff;
    font-size: 0.85rem;
    font-weight: 500;
    padding: 8px 16px;
    border-radius: 999px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.25);
    z-index: 10000;
    pointer-events: none;
    white-space: nowrap;
}

/* ERM SVG Layer */
.erm-svg-layer {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    pointer-events: none;
    z-index: 9999;
}
.erm-line {
    fill: none;
    stroke-width: 3px;
    stroke-linecap: round;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.1));
}
.erm-line-cause { stroke: #f97316; }
.erm-line-rule { stroke: #3b82f6; }
.erm-line-major { stroke: #10b981; }

.erm-rel-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    font-size: 0.85rem;
}

.erm-rel-icon.icon-orange { background: #ffedd5; color: #f97316; }
.erm-rel-icon.icon-blue { background: #dbeafe; color: #3b82f6; }
.erm-rel-icon.icon-gray { background: #d1fae5; color: #10b981; }

.erm-rel-text {
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--text-dark);
}

.erm-relation-cancel {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    transition: all 0.2s;
    font-family: inherit;
    margin-top: 4px;
}

.erm-relation-cancel:hover {
    background: #f1f5f9;
    color: #ef4444;
}