/* ============================================================
   LHEN-ASEAN Collaborative Editor — Academic Pressroom
   ============================================================ */

/* --- CSS Variables --- */
:root {
    --ink:               #1a1a2e;
    --paper:             #f5f0e8;
    --paper-dark:        #e8e0d0;
    --accent:            #8b4513;
    --status-pending:    #d4a017;
    --status-processing: #2563eb;
    --status-done:       #16a34a;
    --status-error:      #dc2626;
    --divider:           #c4b89a;

    --font-heading: 'Cormorant Garamond', Georgia, serif;
    --font-body:    'IBM Plex Sans', system-ui, sans-serif;
    --font-mono:    'IBM Plex Mono', 'Courier New', monospace;

    --radius-sm:  4px;
    --radius-md:  8px;
    --radius-lg:  16px;
    --radius-pill: 24px;

    --topbar-height: 48px;
    --shadow-card:   0 2px 8px rgba(26, 26, 46, 0.10), 0 1px 2px rgba(26, 26, 46, 0.08);
    --shadow-pdf:    0 4px 24px rgba(26, 26, 46, 0.18), 0 1px 4px rgba(26, 26, 46, 0.10);
}

/* --- Reset & Base --- */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font-body);
    font-size: 15px;
    color: var(--ink);
    /* Subtle paper texture via radial gradient noise */
    background-color: var(--paper);
    background-image:
        radial-gradient(ellipse at 20% 20%, rgba(139, 69, 19, 0.04) 0%, transparent 60%),
        radial-gradient(ellipse at 80% 80%, rgba(196, 184, 154, 0.08) 0%, transparent 60%),
        radial-gradient(circle at 50% 50%, rgba(245, 240, 232, 0) 0%, rgba(232, 224, 208, 0.15) 100%);
    line-height: 1.5;
}

/* --- Animations --- */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(12px); }
    to   { opacity: 1; transform: translateY(0); }
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0.5; }
}

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

/* --- Auth Screens --- */
.auth-screen {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--paper);
    background-image:
        radial-gradient(ellipse at 25% 35%, rgba(139, 69, 19, 0.05) 0%, transparent 55%),
        radial-gradient(ellipse at 75% 65%, rgba(196, 184, 154, 0.10) 0%, transparent 55%);
    z-index: 100;
}

[hidden] {
    display: none !important;
}

.auth-card {
    width: 100%;
    max-width: 400px;
    padding: 48px 40px 40px;
    background: linear-gradient(160deg, #ffffff 0%, var(--paper) 100%);
    border: 1px solid var(--divider);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card), 0 8px 40px rgba(26, 26, 46, 0.08);
    animation: fadeIn 0.35s ease both;
    text-align: center;
}

.auth-title {
    font-family: var(--font-heading);
    font-size: 2.4rem;
    font-weight: 700;
    color: var(--ink);
    letter-spacing: 0.02em;
    margin-bottom: 6px;
}

.auth-subtitle {
    font-family: var(--font-body);
    font-size: 0.95rem;
    font-weight: 400;
    color: rgba(26, 26, 46, 0.55);
    margin-bottom: 32px;
    letter-spacing: 0.03em;
}

.auth-card form {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.auth-card input[type="password"],
.auth-card input[type="text"] {
    width: 100%;
    padding: 10px 0;
    font-family: var(--font-body);
    font-size: 1rem;
    color: var(--ink);
    background: transparent;
    border: none;
    border-bottom: 2px solid var(--divider);
    border-radius: 0;
    outline: none;
    text-align: center;
    transition: border-color 0.2s ease;
}

.auth-card input[type="password"]::placeholder,
.auth-card input[type="text"]::placeholder {
    color: rgba(26, 26, 46, 0.35);
    font-style: italic;
}

.auth-card input[type="password"]:focus,
.auth-card input[type="text"]:focus {
    border-bottom-color: var(--accent);
}

.auth-card button[type="submit"] {
    padding: 11px 32px;
    font-family: var(--font-body);
    font-size: 0.9rem;
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--paper);
    background-color: var(--accent);
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background-color 0.18s ease, transform 0.12s ease, box-shadow 0.18s ease;
    box-shadow: 0 2px 8px rgba(139, 69, 19, 0.25);
    align-self: center;
    min-width: 140px;
}

.auth-card button[type="submit"]:hover {
    background-color: #6b330f;
    transform: translateY(-1px);
    box-shadow: 0 4px 14px rgba(139, 69, 19, 0.35);
}

.auth-card button[type="submit"]:active {
    transform: translateY(0);
    box-shadow: 0 2px 6px rgba(139, 69, 19, 0.20);
}

.error-msg {
    margin-top: 14px;
    font-family: var(--font-mono);
    font-size: 0.78rem;
    color: var(--status-error);
    letter-spacing: 0.02em;
}

/* --- Top Bar --- */
#screen-editor {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

#topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--topbar-height);
    padding: 0 20px;
    background-color: var(--paper-dark);
    border-bottom: 1px solid var(--divider);
    flex-shrink: 0;
    gap: 16px;
}

.topbar-left {
    display: flex;
    align-items: center;
    min-width: 0;
}

.topbar-title {
    font-family: var(--font-heading);
    font-size: 1.15rem;
    font-weight: 600;
    color: var(--ink);
    letter-spacing: 0.02em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.topbar-center {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 1;
}

.topbar-right {
    display: flex;
    align-items: center;
    min-width: 0;
    font-family: var(--font-mono);
    font-size: 0.78rem;
    color: rgba(26, 26, 46, 0.55);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* --- Queue Badge --- */
.queue-badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 12px;
    font-family: var(--font-mono);
    font-size: 0.72rem;
    font-weight: 500;
    letter-spacing: 0.04em;
    color: rgba(26, 26, 46, 0.65);
    background-color: rgba(196, 184, 154, 0.30);
    border: 1px solid var(--divider);
    border-radius: var(--radius-pill);
    white-space: nowrap;
    transition: background-color 0.3s, color 0.3s;
}

.queue-badge.pulse {
    color: var(--status-processing);
    background-color: rgba(37, 99, 235, 0.10);
    border-color: rgba(37, 99, 235, 0.30);
    animation: pulse 1.5s ease-in-out infinite;
}

/* --- Loop Heartbeat Badge --- */
.loop-badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 10px;
    margin-left: 8px;
    font-family: var(--font-mono);
    font-size: 0.68rem;
    font-weight: 400;
    letter-spacing: 0.03em;
    color: rgba(26, 26, 46, 0.50);
    background-color: rgba(196, 184, 154, 0.20);
    border: 1px solid rgba(196, 184, 154, 0.40);
    border-radius: var(--radius-pill);
    white-space: nowrap;
}

.loop-badge.online {
    color: var(--status-done);
    border-color: rgba(22, 163, 74, 0.30);
}

.loop-badge.stale {
    color: var(--status-pending);
    border-color: rgba(212, 160, 23, 0.30);
}

.loop-badge.offline {
    color: var(--status-error);
    border-color: rgba(220, 38, 38, 0.30);
}

/* --- Sync Button --- */
/* --- Active Users --- */
.active-users-badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 10px;
    margin-right: 10px;
    font-family: var(--font-mono);
    font-size: 0.68rem;
    font-weight: 400;
    letter-spacing: 0.03em;
    color: var(--status-done);
    background-color: rgba(22, 163, 74, 0.08);
    border: 1px solid rgba(22, 163, 74, 0.25);
    border-radius: var(--radius-pill);
    white-space: nowrap;
}

.active-users-badge:empty {
    display: none;
}

.sync-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    margin-left: 6px;
    font-size: 1.1rem;
    color: var(--accent);
    background: none;
    border: 1px solid var(--divider);
    border-radius: 50%;
    cursor: pointer;
    transition: background-color 0.15s, transform 0.3s;
    flex-shrink: 0;
}

.sync-btn:hover {
    background-color: rgba(139, 69, 19, 0.10);
}

.sync-btn.syncing {
    animation: spin 0.6s linear;
    pointer-events: none;
}

/* --- Main Content Split Pane --- */
#main-content {
    display: grid;
    grid-template-columns: 35% 1fr;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

/* --- Chat Panel --- */
#chat-panel {
    display: flex;
    flex-direction: column;
    height: 100%;
    border-right: 1px solid var(--divider);
    background-color: var(--paper);
    overflow: hidden;
}

#chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px 8px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    scroll-behavior: smooth;
}

#chat-messages::-webkit-scrollbar {
    width: 6px;
}

#chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

#chat-messages::-webkit-scrollbar-thumb {
    background: var(--divider);
    border-radius: 3px;
}

/* --- Chat Cards --- */
.chat-card {
    margin: 4px 6px;
    padding: 12px 14px;
    background: linear-gradient(135deg, #ffffff 0%, var(--paper) 100%);
    border-radius: var(--radius-md);
    border-left: 3px solid var(--divider);
    box-shadow: var(--shadow-card);
    transition: border-color 0.2s;
}

.chat-card.status-pending {
    border-left-color: var(--status-pending);
}

.chat-card.status-processing {
    border-left-color: var(--status-processing);
}

.chat-card.status-done {
    border-left-color: var(--status-done);
}

.chat-card.status-error {
    border-left-color: var(--status-error);
}

.chat-meta {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 5px;
    gap: 8px;
}

.chat-user {
    font-family: var(--font-mono);
    font-size: 0.72rem;
    font-weight: 500;
    color: var(--accent);
    letter-spacing: 0.03em;
}

.chat-time {
    font-family: var(--font-mono);
    font-size: 0.68rem;
    color: rgba(26, 26, 46, 0.40);
    letter-spacing: 0.02em;
    flex-shrink: 0;
}

.chat-text {
    font-family: var(--font-body);
    font-size: 0.88rem;
    color: var(--ink);
    line-height: 1.45;
    margin-bottom: 6px;
}

.chat-result {
    font-family: var(--font-mono);
    font-size: 0.78rem;
    font-style: italic;
    color: rgba(26, 26, 46, 0.50);
    margin-top: 5px;
    padding-top: 5px;
    border-top: 1px solid rgba(196, 184, 154, 0.40);
    line-height: 1.4;
}

.chat-status {
    margin-top: 7px;
}

/* --- Status Badges --- */
.status-badge {
    display: inline-flex;
    align-items: center;
    padding: 2px 8px;
    font-family: var(--font-mono);
    font-size: 0.68rem;
    font-weight: 500;
    letter-spacing: 0.04em;
    border-radius: var(--radius-pill);
}

.badge-pending {
    background-color: rgba(212, 160, 23, 0.15);
    color: var(--status-pending);
}

.badge-processing {
    background-color: rgba(37, 99, 235, 0.12);
    color: var(--status-processing);
    animation: pulse 1.4s ease-in-out infinite;
}

.badge-done {
    background-color: rgba(22, 163, 74, 0.12);
    color: var(--status-done);
}

.badge-error {
    background-color: rgba(220, 38, 38, 0.12);
    color: var(--status-error);
}

/* --- Chat Input Area --- */
#chat-input-area {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 12px 14px;
    background-color: var(--paper-dark);
    border-top: 1px solid var(--divider);
}

#chat-input {
    width: 100%;
    padding: 10px 12px;
    font-family: var(--font-body);
    font-size: 0.88rem;
    color: var(--ink);
    background-color: #ffffff;
    border: 1px solid var(--divider);
    border-radius: var(--radius-md);
    outline: none;
    resize: none;
    line-height: 1.45;
    transition: border-color 0.18s ease, box-shadow 0.18s ease;
}

#chat-input::placeholder {
    color: rgba(26, 26, 46, 0.35);
    font-style: italic;
}

#chat-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(139, 69, 19, 0.12);
}

#chat-input:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

#btn-send {
    align-self: flex-end;
    padding: 8px 22px;
    font-family: var(--font-body);
    font-size: 0.82rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--paper);
    background-color: var(--accent);
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background-color 0.18s ease, transform 0.12s ease;
    box-shadow: 0 1px 4px rgba(139, 69, 19, 0.22);
}

#btn-send:hover:not(:disabled) {
    background-color: #6b330f;
    transform: translateY(-1px);
}

#btn-send:active:not(:disabled) {
    transform: translateY(0);
}

#btn-send:disabled {
    opacity: 0.50;
    cursor: not-allowed;
    transform: none;
}

/* --- Document Panel --- */
#doc-panel {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
    background-color: #f0ebe0;
}

/* --- Main Tabs (top-level) --- */
#main-tabs-bar {
    display: flex;
    flex-direction: row;
    background-color: var(--ink);
    flex-shrink: 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.main-tab {
    padding: 9px 18px;
    font-family: var(--font-body);
    font-size: 0.82rem;
    font-weight: 500;
    color: rgba(245, 240, 232, 0.55);
    background: none;
    border: none;
    border-bottom: 3px solid transparent;
    cursor: pointer;
    white-space: nowrap;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    transition: color 0.15s, border-bottom-color 0.15s;
}

.main-tab:hover {
    color: rgba(245, 240, 232, 0.85);
}

.main-tab.active {
    color: var(--paper);
    border-bottom-color: var(--accent);
}

.tab-panel {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

.tab-panel[hidden] {
    display: none !important;
}

.panel-scroll {
    flex: 1;
    overflow-y: auto;
    padding: 28px 36px;
    background: white;
    margin: 12px;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-pdf);
    font-family: var(--font-body);
    font-size: 0.88rem;
    line-height: 1.6;
    color: var(--ink);
}

.panel-scroll h2 {
    font-family: var(--font-heading);
    font-size: 1.4rem;
    font-weight: 700;
    margin: 24px 0 12px;
    color: var(--ink);
    border-bottom: 2px solid var(--divider);
    padding-bottom: 6px;
}

.panel-scroll h2:first-child {
    margin-top: 0;
}

.panel-scroll h3 {
    font-family: var(--font-heading);
    font-size: 1.1rem;
    font-weight: 600;
    margin: 16px 0 8px;
    color: var(--accent);
}

.panel-scroll table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.82rem;
    margin: 10px 0 16px;
}

.panel-scroll th,
.panel-scroll td {
    border: 1px solid var(--divider);
    padding: 6px 10px;
    text-align: left;
    vertical-align: top;
}

.panel-scroll th {
    background-color: var(--paper-dark);
    font-weight: 600;
}

/* Checklist items */
.checklist-item {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 8px 0;
    border-bottom: 1px solid rgba(196, 184, 154, 0.25);
}

.checklist-item input[type="checkbox"] {
    margin-top: 3px;
    accent-color: var(--accent);
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

.checklist-item label {
    flex: 1;
}

.checklist-item .checklist-detail {
    font-size: 0.78rem;
    color: rgba(26, 26, 46, 0.55);
    font-family: var(--font-mono);
    margin-top: 2px;
}

/* Score cards */
.score-card {
    padding: 14px 16px;
    margin: 8px 0;
    border-radius: var(--radius-md);
    border-left: 4px solid var(--divider);
    background: linear-gradient(135deg, #ffffff 0%, var(--paper) 100%);
}

.score-card.score-high {
    border-left-color: var(--status-done);
}

.score-card.score-mid {
    border-left-color: var(--status-pending);
}

.score-card.score-low {
    border-left-color: var(--status-error);
}

.score-card h4 {
    font-family: var(--font-body);
    font-size: 0.9rem;
    font-weight: 600;
    margin: 0 0 6px;
}

.score-card p {
    margin: 4px 0;
    font-size: 0.84rem;
}

.score-card .action {
    font-family: var(--font-mono);
    font-size: 0.78rem;
    color: var(--accent);
    margin-top: 6px;
}

/* --- Section Tabs Bar --- */
#section-tabs-bar {
    display: flex;
    align-items: center;
    background-color: var(--paper-dark);
    border-bottom: 1px solid var(--divider);
    flex-shrink: 0;
}

#section-tabs {
    display: flex;
    flex-direction: row;
    gap: 0;
    overflow-x: auto;
    flex: 1;
    -webkit-overflow-scrolling: touch;
}

#section-tabs::-webkit-scrollbar {
    height: 3px;
}

#section-tabs::-webkit-scrollbar-thumb {
    background: var(--divider);
}

.tab {
    padding: 10px 18px;
    font-family: var(--font-heading);
    font-size: 0.88rem;
    font-weight: 400;
    color: rgba(26, 26, 46, 0.60);
    background: none;
    border: none;
    border-bottom: 3px solid transparent;
    cursor: pointer;
    white-space: nowrap;
    transition: color 0.15s ease, border-bottom-color 0.15s ease, font-weight 0.15s ease;
    letter-spacing: 0.02em;
}

.tab:hover {
    color: var(--ink);
    background-color: rgba(196, 184, 154, 0.20);
}

.tab.active {
    color: var(--accent);
    font-weight: 600;
    border-bottom-color: var(--accent);
    background-color: rgba(139, 69, 19, 0.04);
}

.download-btn {
    display: inline-flex;
    align-items: center;
    padding: 6px 14px;
    margin-right: 10px;
    font-family: var(--font-mono);
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--accent);
    background: none;
    border: 1px solid var(--accent);
    border-radius: var(--radius-sm);
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
    transition: background-color 0.15s, color 0.15s;
    flex-shrink: 0;
}

.download-btn:hover {
    background-color: var(--accent);
    color: var(--paper);
}

/* --- Markdown Viewer --- */
#markdown-viewer {
    flex: 1;
    overflow-y: auto;
    padding: 32px 40px;
    background: white;
    box-shadow: var(--shadow-pdf);
    margin: 16px;
    border-radius: var(--radius-md);
    font-family: 'Times New Roman', Georgia, serif;
    font-size: 11pt;
    line-height: 1.5;
    color: var(--ink);
    text-align: justify;
    user-select: text;
}

#markdown-viewer::-webkit-scrollbar {
    width: 8px;
}

#markdown-viewer::-webkit-scrollbar-track {
    background: rgba(196, 184, 154, 0.15);
}

#markdown-viewer::-webkit-scrollbar-thumb {
    background: var(--divider);
    border-radius: 4px;
}

#markdown-viewer h1 {
    font-family: 'Times New Roman', Georgia, serif;
    font-size: 14pt;
    font-weight: bold;
    margin: 18pt 0 6pt;
}

#markdown-viewer h2 {
    font-family: 'Times New Roman', Georgia, serif;
    font-size: 12pt;
    font-weight: bold;
    margin: 12pt 0 6pt;
}

#markdown-viewer h3 {
    font-family: 'Times New Roman', Georgia, serif;
    font-size: 11pt;
    font-weight: bold;
    font-style: italic;
    margin: 6pt 0 3pt;
}

#markdown-viewer p {
    margin: 0 0 6pt;
}

#markdown-viewer table {
    border-collapse: collapse;
    width: 100%;
    font-size: 9pt;
    margin: 6pt 0;
}

#markdown-viewer th,
#markdown-viewer td {
    border: 1px solid #999;
    padding: 3pt 5pt;
    text-align: left;
    vertical-align: top;
}

#markdown-viewer th {
    font-weight: bold;
    background-color: #f5f0e8;
}

#markdown-viewer img {
    max-width: 100%;
    display: block;
    margin: 8pt auto;
}

/* --- Edit Highlights --- */
.edit-highlight {
    background-color: rgba(37, 99, 235, 0.12);
    border-bottom: 2px solid var(--status-processing);
    padding: 1px 2px;
    border-radius: 2px;
    cursor: pointer;
    transition: background-color 0.2s;
    position: relative;
}

.edit-highlight:hover {
    background-color: rgba(37, 99, 235, 0.25);
}

.edit-highlight[data-req-id]::after {
    content: attr(data-user);
    position: absolute;
    top: -18px;
    left: 0;
    font-family: var(--font-mono);
    font-size: 0.6rem;
    color: var(--status-processing);
    background: rgba(37, 99, 235, 0.08);
    padding: 1px 4px;
    border-radius: 2px;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s;
}

.edit-highlight:hover[data-req-id]::after {
    opacity: 1;
}

/* Highlight the chat card when its edit is focused */
.chat-card.highlight-linked {
    box-shadow: 0 0 0 2px var(--status-processing), var(--shadow-card);
}

/* --- Responsive: Mobile --- */
@media (max-width: 768px) {
    #main-content {
        grid-template-columns: 1fr;
        grid-template-rows: 1fr auto;
    }

    #chat-panel {
        border-right: none;
        border-top: 1px solid var(--divider);
        max-height: 40vh;
        order: 2;
    }

    #doc-panel {
        order: 1;
        min-height: 0;
    }

    #markdown-viewer {
        margin: 8px;
        padding: 20px 16px;
    }

    .auth-card {
        margin: 16px;
        padding: 36px 24px 28px;
    }

    .auth-title {
        font-size: 2rem;
    }

    #topbar {
        padding: 0 12px;
    }

    .topbar-title {
        font-size: 1rem;
    }

    .topbar-right {
        display: none;
    }

    #section-tabs {
        -webkit-overflow-scrolling: touch;
    }

    .tab {
        padding: 8px 14px;
        font-size: 0.82rem;
    }
}
