/* ── Variables ────────────────────────────────────────────────────── */

:root {
    --bg: #fbf7f0;
    --bg-subtle: #f3ede4;
    --text: #1a1a1a;
    --text-secondary: #5c5c5c;
    --text-dim: #8a8a8a;
    --rule: #c8bfb0;
    --rule-light: #ddd5c8;
    --accent: #8b2500;
    --accent-hover: #a83000;
    --green: #2d6a30;
    --red: #9b2c2c;
    --serif: 'Libre Baskerville', 'Georgia', serif;
    --display: 'Playfair Display', 'Georgia', serif;
}

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

body {
    font-family: var(--serif);
    background: var(--bg);
    color: var(--text);
    line-height: 1.7;
    -webkit-font-smoothing: antialiased;
    height: 100vh;
    overflow: hidden;
}

/* ── Login ────────────────────────────────────────────────────────── */

.login-screen {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    z-index: 200;
    overflow: hidden;
}

.login-grain {
    position: absolute;
    inset: 0;
    opacity: 0.02;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
    background-size: 200px;
    pointer-events: none;
}

.login-brand {
    display: none;
}

.login-card {
    text-align: center;
    max-width: 540px;
    padding: 2rem;
}

.login-rule {
    width: 48px;
    height: 1px;
    background: var(--rule);
    margin: 0 auto 2rem;
    animation: loginFade 1s ease both;
    animation-delay: 0.3s;
}

.login-tagline {
    font-family: 'Instrument Serif', serif;
    font-size: 3.8rem;
    font-weight: 400;
    color: var(--text);
    line-height: 1.1;
    letter-spacing: -0.01em;
    margin-bottom: 1.8rem;
    animation: loginFade 0.8s ease both;
    animation-delay: 0.1s;
}

.login-subtitle {
    font-family: 'DM Sans', sans-serif;
    font-size: 0.88rem;
    font-weight: 300;
    line-height: 1.8;
    color: var(--text-secondary);
    max-width: 420px;
    margin: 0 auto 1rem;
    animation: loginFade 0.8s ease both;
    animation-delay: 0.5s;
}

.login-subtle {
    font-family: 'DM Sans', sans-serif;
    font-size: 0.78rem;
    font-weight: 300;
    color: var(--text-dim);
    margin-bottom: 3rem;
    animation: loginFade 0.8s ease both;
    animation-delay: 0.7s;
}

.login-btn {
    font-family: 'DM Sans', sans-serif;
    font-size: 0.78rem;
    font-weight: 400;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    padding: 0.9rem 2.8rem;
    background: var(--text);
    color: var(--bg);
    border: 1px solid var(--text);
    cursor: pointer;
    transition: all 0.3s ease;
    animation: loginFade 0.8s ease both;
    animation-delay: 0.9s;
}

.login-btn:hover {
    opacity: 0.85;
}

@keyframes loginFade {
    from { opacity: 0; transform: translateY(12px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ── App layout ───────────────────────────────────────────────────── */

.app-layout {
    display: flex;
    height: 100vh;
}

/* ── Sidebar ──────────────────────────────────────────────────────── */

.sidebar {
    width: 240px;
    min-width: 240px;
    border-right: 1px solid var(--rule);
    display: flex;
    flex-direction: column;
    padding: 1rem;
    gap: 0.75rem;
    overflow-y: auto;
}

.sidebar-header {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--rule-light);
}

.app-brand {
    font-family: var(--display);
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text);
    letter-spacing: -0.01em;
}

.sidebar-btn {
    padding: 7px 12px;
    background: var(--bg);
    border: 1px solid var(--rule);
    font-family: var(--serif);
    font-size: 0.72rem;
    color: var(--text);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
    text-align: center;
    letter-spacing: 0.02em;
    width: 100%;
}

.sidebar-btn:hover {
    background: var(--bg-subtle);
    border-color: var(--text-secondary);
}

.sidebar-btn--new {
    background: var(--text);
    color: var(--bg);
    border-color: var(--text);
}

.sidebar-btn--new:hover {
    opacity: 0.85;
    background: var(--text);
}

.sidebar-btn--logout {
    margin-top: auto;
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 0.7rem;
}

.sidebar-btn--logout:hover {
    color: var(--text-secondary);
    background: none;
    border: none;
}

/* ── Query list ───────────────────────────────────────────────────── */

.query-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.query-item {
    display: flex;
    align-items: center;
    padding: 6px 8px;
    font-size: 0.7rem;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 3px;
    line-height: 1.4;
}

.query-item .query-text {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.query-item .remove-btn {
    background: none;
    border: none;
    color: var(--red);
    cursor: pointer;
    font-size: 1rem;
    padding: 0 0.3rem;
    flex-shrink: 0;
    opacity: 0;
    transition: opacity 0.15s;
}

.query-item:hover .remove-btn {
    opacity: 1;
}

.query-item .remove-btn:hover {
    color: #c53030;
}

.query-item:hover {
    background: var(--bg-subtle);
    color: var(--text);
}

.query-item.active {
    background: var(--bg-subtle);
    color: var(--text);
    font-weight: 700;
}


/* ── Delete confirmation popover ──────────────────────────────────── */

.delete-confirm {
    position: absolute;
    background: #fff;
    border: 1px solid var(--rule);
    border-radius: 4px;
    padding: 6px 10px;
    display: flex;
    align-items: center;
    gap: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    z-index: 100;
    font-size: 0.7rem;
}

.delete-confirm-text {
    color: var(--text-secondary);
}

.delete-confirm-yes,
.delete-confirm-no {
    font-family: var(--serif);
    font-size: 0.65rem;
    padding: 2px 8px;
    border: 1px solid var(--rule);
    border-radius: 3px;
    cursor: pointer;
    background: var(--bg);
    color: var(--text);
}

.delete-confirm-yes {
    color: var(--red);
    border-color: var(--red);
}

.delete-confirm-yes:hover {
    background: var(--red);
    color: #fff;
}

.delete-confirm-no:hover {
    background: var(--bg-subtle);
}

.query-spinner {
    display: inline-block;
    width: 12px;
    height: 12px;
    border: 2px solid var(--rule);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-right: 5px;
    vertical-align: middle;
    flex-shrink: 0;
}

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

/* ── Main area ────────────────────────────────────────────────────── */

.main-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.main-split {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* ── Input panel ──────────────────────────────────────────────────── */

.input-panel {
    width: 50%;
    transition: width 0.3s ease;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    border-right: 1px solid var(--rule-light);
    overflow-y: auto;
}

.mode-toggle {
    display: flex;
    gap: 0;
}

.mode-btn {
    flex: 1;
    padding: 7px 12px;
    background: var(--bg);
    border: 1px solid var(--rule);
    font-family: var(--serif);
    font-size: 0.75rem;
    color: var(--text-secondary);
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
    letter-spacing: 0.02em;
}

.mode-btn + .mode-btn { border-left: none; }

.mode-btn.active {
    background: var(--text);
    color: var(--bg);
    border-color: var(--text);
}

.mode-btn:hover:not(.active) {
    background: var(--bg-subtle);
}

.query-display-text {
    font-family: 'SF Mono', 'Menlo', 'Consolas', monospace;
    font-size: 0.8rem;
    line-height: 1.6;
    color: var(--text-secondary);
    background: var(--bg-subtle);
    border: 1px solid var(--rule-light);
    border-radius: 4px;
    padding: 0.6rem 0.8rem;
    overflow-y: auto;
}

.query-input:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.mode-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.query-input-wrap {
    flex: 1;
    position: relative;
    display: flex;
    flex-direction: column;
}

.query-input {
    flex: 1;
    min-height: 10rem;
    background: #fff;
    border: 1px solid var(--rule);
    color: var(--text);
    padding: 0.5rem 0.6rem;
    padding-bottom: 2.5rem;
    font-family: var(--serif);
    font-size: 0.85rem;
    line-height: 1.6;
    resize: none;
    outline: none;
}

.query-input:focus {
    border-color: var(--text-secondary);
}

.query-input-footer {
    position: absolute;
    bottom: 0.4rem;
    left: 0.5rem;
    right: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.attach-btn {
    color: var(--text-dim);
    cursor: pointer;
    padding: 2px;
    line-height: 1;
    transition: color 0.15s;
}

.attach-btn:hover {
    color: var(--text);
}

.file-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
}

.file-tag {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 3px 8px;
    background: var(--bg-subtle);
    border: 1px solid var(--rule-light);
    border-radius: 3px;
    font-family: 'SF Mono', 'Menlo', 'Consolas', monospace;
    font-size: 0.65rem;
    color: var(--text-secondary);
}

.file-tag-remove {
    background: none;
    border: none;
    color: var(--red);
    cursor: pointer;
    font-size: 0.8rem;
    padding: 0;
    line-height: 1;
}

.submit-btn {
    padding: 0.4rem 1.2rem;
    background: var(--text);
    color: var(--bg);
    border: none;
    font-family: var(--serif);
    font-size: 0.75rem;
    cursor: pointer;
    letter-spacing: 0.03em;
    transition: opacity 0.15s;
}

.submit-btn:hover { opacity: 0.85; }
.submit-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Parent context block (child queries) ────────────────────────── */

.parent-context {
    background: var(--bg-subtle);
    border: 1px solid var(--rule-light);
    border-radius: 4px;
    max-height: 220px;
    overflow-y: auto;
    font-size: 0.78rem;
    line-height: 1.6;
}

.parent-context-label {
    font-size: 0.6rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-dim);
    padding: 0.6rem 0.8rem 0;
}

.parent-context-body {
    color: var(--text-secondary);
    padding: 0.4rem 0.8rem 0.6rem;
}

.context-card {
    background: var(--bg-subtle);
    border: 1px solid var(--rule-light);
    border-radius: 6px;
    padding: 0.5rem 0.7rem;
    margin-bottom: 0.4rem;
}

.context-card-role {
    font-size: 0.62rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-tertiary);
    margin-bottom: 0.25rem;
}

.parent-context-query {
    font-style: italic;
}

.parent-context-result {
    font-size: 0.72rem;
    line-height: 1.6;
}

.parent-context-result p { margin-bottom: 0.4em; }
.parent-context-result h1,
.parent-context-result h2,
.parent-context-result h3 {
    font-family: var(--display);
    font-size: 0.82rem;
    margin: 0.6em 0 0.2em;
}

/* When in child-draft mode, context takes 70% and textarea 30% */
.input-panel.child-mode .parent-context {
    flex: 7;
    max-height: none;
    min-height: 0;
}

.input-panel.child-mode .query-input-wrap,
.input-panel.child-mode .query-display-text {
    flex: 3;
    min-height: 0;
}

.input-panel.child-mode .query-input {
    min-height: 0;
}

/* ── Go Deeper button ────────────────────────────────────────────── */

.go-deeper-btn {
    padding: 7px 16px;
    background: var(--bg);
    border: 1px solid var(--rule);
    font-family: var(--serif);
    font-size: 0.75rem;
    color: var(--text);
    cursor: pointer;
    letter-spacing: 0.02em;
    transition: background 0.15s, border-color 0.15s, opacity 0.15s;
    align-self: flex-start;
    position: sticky;
    bottom: 0;
    margin-top: 0.5rem;
    z-index: 2;
}

.go-deeper-btn:hover:not(:disabled) {
    background: var(--bg-subtle);
    border-color: var(--text-secondary);
}

.go-deeper-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

/* ── Sidebar tree indentation ────────────────────────────────────── */

.query-item[data-depth="1"] { padding-left: 22px; }
.query-item[data-depth="2"] { padding-left: 38px; }
.query-item[data-depth="3"] { padding-left: 54px; }

.query-item.draft .query-text {
    font-style: italic;
    opacity: 0.7;
}

.mobile-only { display: none !important; }

/* ── Progress bar ─────────────────────────────────────────────────── */

.progress-area {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    margin-bottom: 1rem;
    max-width: 280px;
    align-self: center;
    width: 100%;
}

.progress-bar {
    height: 3px;
    background: var(--rule-light);
    border-radius: 2px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    width: 0%;
    background: var(--accent);
    border-radius: 2px;
    transition: width 2s ease;
}

.progress-text {
    font-size: 0.7rem;
    color: var(--text-dim);
    letter-spacing: 0.02em;
}

/* ── Output panel ─────────────────────────────────────────────────── */

.main-split.viewing .input-panel {
    width: 35%;
}

.main-split.viewing .output-panel {
    width: 65%;
}

.output-panel {
    position: relative;
    width: 50%;
    transition: width 0.3s ease;
    padding: 1.5rem;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.copy-btn {
    position: absolute;
    top: 0.7rem;
    right: 0.7rem;
    z-index: 2;
    background: var(--bg-subtle);
    border: 1px solid var(--rule-light);
    border-radius: 4px;
    padding: 5px 6px;
    cursor: pointer;
    color: var(--text-secondary);
    opacity: 0.6;
    transition: opacity 0.15s;
}

.copy-btn:hover { opacity: 1; }
.copy-btn.copied { color: #2a7d3f; opacity: 1; }

.output-panel.centered {
    justify-content: center;
}

.output-content {
    font-size: 0.85rem;
    line-height: 1.8;
}

.output-placeholder {
    color: var(--text-dim);
    font-style: italic;
}

.output-content h1, .output-content h2, .output-content h3 {
    font-family: var(--display);
    margin: 1.2em 0 0.4em;
    line-height: 1.3;
}

.output-content h1 { font-size: 1.4rem; }
.output-content h2 { font-size: 1.15rem; }
.output-content h3 { font-size: 1rem; }

.output-content p { margin-bottom: 0.8em; }

.output-content ul, .output-content ol {
    margin: 0.5em 0 0.8em 1.5em;
}

.output-content li { margin-bottom: 0.3em; }

.output-content blockquote {
    border-left: 3px solid var(--rule);
    padding-left: 1em;
    color: var(--text-secondary);
    margin: 0.8em 0;
}

.output-content code {
    background: var(--bg-subtle);
    padding: 0.15em 0.35em;
    font-size: 0.82em;
    border-radius: 3px;
}

.output-content pre {
    background: var(--bg-subtle);
    padding: 0.8em 1em;
    overflow-x: auto;
    margin: 0.8em 0;
    border-radius: 3px;
}

.output-content pre code {
    background: none;
    padding: 0;
}

.output-content strong { font-weight: 700; }

.output-content em { font-style: italic; }

/* ── Mobile topbar & hamburger ────────────────────────────────────── */

.mobile-topbar {
    display: none;
}

.mobile-topbar-left {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.mobile-avatar-menu {
    position: relative;
}

.mobile-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    border: 1px solid var(--rule-light);
}

.mobile-avatar-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    margin-top: 0.3rem;
    background: var(--bg);
    border: 1px solid var(--rule);
    min-width: 120px;
    z-index: 60;
}

.mobile-avatar-dropdown.hidden {
    display: none;
}

.mobile-avatar-dropdown .mobile-menu-item {
    padding: 10px 16px;
    background: none;
    border: none;
    font-family: var(--serif);
    font-size: 0.78rem;
    cursor: pointer;
    text-align: left;
    display: block;
    width: 100%;
}

.mobile-menu-item--logout {
    color: var(--text-dim);
}

.mobile-menu-item--logout:hover {
    color: var(--red);
    background: var(--bg-subtle);
}

.mobile-queries-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0.3rem;
    line-height: 1;
    transition: color 0.15s;
}

.mobile-queries-btn:hover {
    color: var(--text);
}

.mobile-query-list {
    display: flex;
    flex-direction: column;
}

.mobile-query-item {
    padding: 12px 1rem;
    background: none;
    border: none;
    border-bottom: 1px solid var(--rule-light);
    font-family: var(--serif);
    font-size: 0.82rem;
    color: var(--text-secondary);
    cursor: pointer;
    text-align: left;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.mobile-query-item:hover {
    background: var(--bg-subtle);
    color: var(--text);
}

.mobile-query-item.active {
    color: var(--text);
    font-weight: 700;
}

/* ── Mobile queries panel ─────────────────────────────────────────── */

.mobile-queries-panel {
    display: none;
    flex-direction: column;
    flex: 1;
    overflow-y: auto;
}

.mobile-queries-panel:not(.hidden) {
    display: flex;
}

.mobile-queries-header {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--rule-light);
}

.mobile-queries-header .sidebar-btn--new {
    width: 100%;
}

/* ── Mobile block ─────────────────────────────────────────────────── */

.mobile-block {
    display: none;
    position: fixed;
    inset: 0;
    background: var(--bg);
    z-index: 999;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-family: 'DM Sans', sans-serif;
    font-size: 0.9rem;
    color: var(--text-secondary);
    line-height: 1.8;
    padding: 2rem;
}

/* ── Mobile ───────────────────────────────────────────────────────── */

@media (max-width: 700px) {
    .app-layout { flex-direction: column; }
    .sidebar { display: none !important; }

    .mobile-topbar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0.75rem 1rem;
        border-bottom: 1px solid var(--rule-light);
    }

    .mobile-topbar .app-brand { display: none; }

    .main-split { flex-direction: column; }
    .input-panel, .output-panel { width: 100% !important; border-right: none; }
    .input-panel { border-bottom: none; flex: none; }

    /* Hide output panel when composing (no query selected) */
    .output-panel { display: none; }
    .main-split.viewing .input-panel { border-bottom: 1px solid var(--rule-light); flex: none; }
    .main-split.viewing .output-panel { display: flex; flex: 1; min-height: 40vh; }

    /* Input fills screen when composing */
    .input-panel { flex: 1; }
    .input-panel .query-input-wrap { flex: 1; }

    .desktop-only { display: none !important; }
    .mobile-only { display: block !important; }

    .login-tagline { font-size: 3rem; margin-bottom: 1.5rem; }
    .login-subtitle { font-size: 0.82rem; }
    .login-brand { left: 1rem; top: 1.2rem; }
}
