/* ── reset ──────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }

/* ── variables (dark default) ───────────────────────────────────────── */
:root {
    --bg:        #0b0a14;
    --surface:   #13101e;
    --surface2:  #1a1628;
    --border:    rgba(155,93,229,.25);
    --accent:    #9b5de5;
    --accent-dk: #7b3fc0;
    --accent2:   #f72585;
    --glow:      rgba(155,93,229,.4);
    --text:      #e8e0ff;
    --muted:     #a89ec0;
    --danger:    #f72585;
    --sidebar-w: 260px;
    --radius:    8px;
    --shadow:    0 2px 12px rgba(0,0,0,.4);
    --header-bg: rgba(11,10,20,.85);
}
[data-theme="light"] {
    --bg:        #f5f2ff;
    --surface:   #ffffff;
    --surface2:  #ede8ff;
    --border:    rgba(155,93,229,.2);
    --accent:    #9b5de5;
    --accent-dk: #7b3fc0;
    --accent2:   #f72585;
    --glow:      rgba(155,93,229,.2);
    --text:      #1a1030;
    --muted:     #4a3f6b;
    --danger:    #d44c47;
    --shadow:    0 1px 8px rgba(0,0,0,.1);
    --header-bg: rgba(245,242,255,.9);
}

/* ── layout ─────────────────────────────────────────────────────────── */
body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 15px;
    color: var(--text);
    background: var(--bg);
    display: flex;
    flex-direction: column;
    height: 100%;
    transition: background .2s, color .2s;
}

/* scanline grid (dark mode only) */
:root body::after {
    content: '';
    position: fixed;
    inset: 0;
    background-image:
        repeating-linear-gradient(0deg, transparent, transparent 2px, rgba(155,93,229,.02) 2px, rgba(155,93,229,.02) 4px),
        repeating-linear-gradient(90deg, transparent, transparent 2px, rgba(155,93,229,.02) 2px, rgba(155,93,229,.02) 4px);
    pointer-events: none;
    z-index: 0;
}
[data-theme="light"] body::after { display: none; }

/* ── top nav ─────────────────────────────────────────────────────────── */
#topnav {
    background: var(--header-bg);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 0 16px;
    height: 52px;
    flex-shrink: 0;
    z-index: 10;
    position: relative;
}
#topnav .logo {
    font-weight: 900;
    font-size: 18px;
    letter-spacing: 3px;
    text-transform: uppercase;
    margin-right: 16px;
    text-decoration: none;
    background: linear-gradient(135deg, var(--text), var(--accent), #c77dff);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}
.tab-btn {
    padding: 6px 14px;
    border: none;
    background: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    color: var(--muted);
    font-family: inherit;
    transition: background .15s, color .15s;
}
.tab-btn:hover  { background: var(--surface2); color: var(--text); }
.tab-btn.active { background: var(--surface2); color: var(--accent); }

/* nav spacer + right-side controls */
#topnav-right {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 8px;
}
#user-badge {
    font-size: 13px;
    font-weight: 600;
    color: var(--muted);
    display: flex;
    align-items: center;
    gap: 5px;
}
#user-badge span { color: var(--accent); }

/* ── theme toggle ────────────────────────────────────────────────────── */
#theme-toggle {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 4px 10px;
    cursor: pointer;
    font-size: 15px;
    color: var(--muted);
    transition: border-color .15s, color .15s;
    font-family: inherit;
}
#theme-toggle:hover { border-color: var(--accent); color: var(--accent); }

/* ── auth screen ─────────────────────────────────────────────────────── */
#auth-screen {
    position: fixed;
    inset: 0;
    background: var(--bg);
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 0;
}
#auth-screen.hidden { display: none; }
.auth-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 36px 40px;
    width: min(420px, 92vw);
    box-shadow: 0 8px 40px rgba(0,0,0,.3), 0 0 60px var(--glow);
}
.auth-logo {
    font-size: 28px;
    font-weight: 900;
    letter-spacing: 4px;
    text-transform: uppercase;
    background: linear-gradient(135deg, var(--text), var(--accent), #c77dff);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    text-align: center;
    margin-bottom: 8px;
}
.auth-tagline {
    text-align: center;
    color: var(--muted);
    font-size: 13px;
    margin-bottom: 28px;
}
.auth-tabs {
    display: flex;
    border-bottom: 1px solid var(--border);
    margin-bottom: 24px;
}
.auth-tab {
    flex: 1;
    text-align: center;
    padding: 8px 0;
    font-size: 14px;
    font-weight: 600;
    color: var(--muted);
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: color .15s, border-color .15s;
    font-family: inherit;
    background: none;
    border-top: none;
    border-left: none;
    border-right: none;
}
.auth-tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.auth-error {
    color: var(--accent2);
    font-size: 13px;
    margin-bottom: 12px;
    padding: 8px 12px;
    background: rgba(247,37,133,.1);
    border-radius: 6px;
    display: none;
}
.auth-error.show { display: block; }

/* ── main wrapper ────────────────────────────────────────────────────── */
#app { display: flex; flex: 1; overflow: hidden; position: relative; z-index: 1; }

/* ── sidebar (spaces) ────────────────────────────────────────────────── */
#sidebar {
    width: var(--sidebar-w);
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    flex-shrink: 0;
}
#sidebar-header {
    padding: 12px 12px 8px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 4px;
}
#sidebar-header span { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); flex: 1; }

/* ── space overview grid ─────────────────────────────────────────────── */
#overview-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 16px;
}
.space-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    cursor: pointer;
    transition: box-shadow .15s, transform .15s, border-color .15s;
    display: flex;
    flex-direction: column;
    gap: 8px;
    position: relative;
}
.space-card:hover { box-shadow: 0 4px 24px var(--glow), 0 0 0 1px var(--accent); transform: translateY(-2px); border-color: var(--accent); }
.space-card .sc-icon { font-size: 28px; }
.space-card .sc-title { font-size: 16px; font-weight: 700; }
.space-card .sc-slug { font-size: 12px; color: var(--accent); }
.space-card .sc-meta { font-size: 12px; color: var(--muted); margin-top: 4px; }
.space-card-new {
    border-style: dashed;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    font-size: 14px;
    gap: 6px;
}
.space-card-new:hover { color: var(--accent); border-color: var(--accent); }

/* visibility badge */
.vis-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 10px;
    position: absolute;
    top: 12px;
    right: 12px;
}
.vis-badge.private { background: rgba(155,93,229,.15); color: var(--accent); }
.vis-badge.public  { background: rgba(247,37,133,.12); color: var(--accent2); }

/* ── subdomain (single-space) mode ──────────────────────────────────── */
body.subdomain-mode #main-area { flex: 1; }
#topnav-space-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--text);
    margin-left: 8px;
}

/* ── slug hint in sidebar ────────────────────────────────────────────── */
.pi-slug {
    font-size: 10px;
    color: var(--accent);
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 1px;
}
.page-item .pi-body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.pi-rename { width: 100%; border: none; outline: none; font-size: 14px; background: transparent; color: var(--text); padding: 0; font-family: inherit; }
#sidebar-list {
    flex: 1;
    overflow-y: auto;
    padding: 6px 0;
}
.page-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 7px 12px;
    cursor: pointer;
    border-radius: var(--radius);
    margin: 1px 6px;
    transition: background .12s;
    position: relative;
}
.page-item:hover { background: var(--surface2); }
.page-item.active { background: rgba(155,93,229,.15); }
.page-item .pi-icon { font-size: 14px; flex-shrink: 0; }
.page-item .pi-title { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.page-item .pi-vis { font-size: 11px; color: var(--muted); flex-shrink: 0; }
.page-item .pi-del {
    display: none;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--danger);
    padding: 2px 4px;
    border-radius: 4px;
    font-size: 14px;
    line-height: 1;
    font-family: inherit;
}
.page-item:hover .pi-del { display: block; }

/* ── main area ───────────────────────────────────────────────────────── */
#main-area {
    flex: 1;
    display: flex;
    overflow: hidden;
}

/* ── inner tab bar ───────────────────────────────────────────────────── */
#inner-tabbar {
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 0 12px;
    height: 44px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
    overflow-x: auto;
}
#subpage-tabs {
    display: flex;
    align-items: center;
    gap: 2px;
}
.inner-tab {
    padding: 5px 14px;
    border: none;
    border-radius: var(--radius);
    background: transparent;
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: background .12s, color .12s;
    position: relative;
    font-family: inherit;
}
.inner-tab:hover  { background: var(--surface2); color: var(--text); }
.inner-tab.active { background: var(--surface2); color: var(--accent); }
.inner-tab .tab-close {
    display: none;
    margin-left: 6px;
    font-size: 11px;
    color: var(--muted);
    line-height: 1;
}
.inner-tab:hover .tab-close { display: inline; }
.inner-tab-sep {
    width: 1px;
    height: 20px;
    background: var(--border);
    margin: 0 4px;
    flex-shrink: 0;
}

/* visibility toggle in tabbar */
#vis-toggle {
    margin-left: auto;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
    cursor: pointer;
    background: none;
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 3px 10px;
    transition: border-color .15s, color .15s;
    font-family: inherit;
}
#vis-toggle:hover { border-color: var(--accent); color: var(--accent); }
#vis-toggle.is-public { color: var(--accent2); border-color: var(--accent2); }

#space-content {
    display: none;
    flex: 1;
    flex-direction: column;
    overflow: hidden;
}

/* ── editor ─────────────────────────────────────────────────────────── */
#editor-pane {
    flex: 1;
    overflow-y: auto;
    padding: 40px max(40px, calc(50% - 380px));
    background: var(--bg);
    display: flex;
    flex-direction: column;
}
#editor-pane.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    gap: 12px;
}
#editor-pane.empty-state .es-icon { font-size: 48px; }
#editor-pane.empty-state p { font-size: 15px; }

#page-title {
    width: 100%;
    border: none;
    background: transparent;
    font-size: 36px;
    font-weight: 700;
    color: var(--text);
    outline: none;
    resize: none;
    line-height: 1.2;
    margin-bottom: 18px;
    padding: 0;
    overflow: hidden;
    min-height: 44px;
    font-family: inherit;
}
#page-title::placeholder { color: var(--muted); opacity: .5; }

#page-content {
    width: 100%;
    border: none;
    background: transparent;
    font-size: 15px;
    color: var(--text);
    outline: none;
    resize: none;
    line-height: 1.7;
    min-height: 300px;
    padding: 0;
    font-family: inherit;
}
#page-content::placeholder { color: var(--muted); opacity: .5; }

/* ── editor toolbar (edit/preview toggle) ────────────────────────────── */
.editor-toolbar {
    display: flex;
    gap: 4px;
    margin-bottom: 18px;
}
.editor-toolbar button {
    padding: 4px 14px;
    font-size: 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    transition: background .15s, color .15s;
    font-family: inherit;
}
.editor-toolbar button.active {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
    box-shadow: 0 0 12px var(--glow);
}

/* ── markdown preview prose ──────────────────────────────────────────── */
#preview-content {
    font-size: 15px;
    line-height: 1.7;
    color: var(--text);
    min-height: 300px;
}
#preview-content h1,
#preview-content h2,
#preview-content h3,
#preview-content h4,
#preview-content h5,
#preview-content h6 {
    margin: 1.4em 0 .5em;
    font-weight: 700;
    line-height: 1.25;
    color: var(--text);
}
#preview-content h1 { font-size: 2em; }
#preview-content h2 { font-size: 1.5em; border-bottom: 1px solid var(--border); padding-bottom: .3em; }
#preview-content h3 { font-size: 1.2em; }
#preview-content p  { margin: .75em 0; }
#preview-content a  { color: var(--accent); text-decoration: underline; }
#preview-content ul,
#preview-content ol { padding-left: 1.6em; margin: .75em 0; }
#preview-content li { margin: .25em 0; }
#preview-content code {
    font-family: monospace;
    font-size: 13px;
    background: var(--surface2);
    padding: 1px 5px;
    border-radius: 4px;
}
#preview-content pre {
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 16px;
    overflow-x: auto;
    margin: 1em 0;
}
#preview-content pre code { background: none; padding: 0; font-size: 13px; }
#preview-content blockquote {
    border-left: 4px solid var(--accent);
    margin: 1em 0;
    padding: .25em 0 .25em 1em;
    color: var(--muted);
}
#preview-content hr { border: none; border-top: 1px solid var(--border); margin: 1.5em 0; }
#preview-content table { border-collapse: collapse; width: 100%; margin: 1em 0; }
#preview-content th,
#preview-content td { border: 1px solid var(--border); padding: 6px 12px; text-align: left; }
#preview-content th { background: var(--surface2); font-weight: 600; }
#preview-content img { max-width: 100%; border-radius: 6px; margin: .5em 0; }

.editor-meta { margin-top: 24px; font-size: 12px; color: var(--muted); }

/* ── links pane ──────────────────────────────────────────────────────── */
#links-pane {
    display: none;
    flex: 1;
    overflow-y: auto;
    padding: 32px 16px;
    flex-direction: column;
    align-items: center;
    background: var(--bg);
}
#links-inner {
    width: 100%;
    max-width: 560px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.links-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
}
.links-header h2 { font-size: 20px; font-weight: 700; }

.link-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    box-shadow: var(--shadow);
    transition: box-shadow .15s, transform .15s, border-color .15s;
    cursor: grab;
}
.link-card:active { cursor: grabbing; }
.link-card:hover  { box-shadow: 0 4px 20px var(--glow); transform: translateY(-1px); border-color: var(--accent); }
.link-card.dragging { opacity: .5; }
.link-drag-handle { color: var(--muted); font-size: 18px; cursor: grab; flex-shrink: 0; }
.link-favicon {
    width: 32px; height: 32px;
    border-radius: 6px;
    background: var(--surface2);
    display: flex; align-items: center; justify-content: center;
    font-size: 18px;
    flex-shrink: 0;
    overflow: hidden;
}
.link-favicon img { width: 100%; height: 100%; object-fit: contain; }
.link-body { flex: 1; min-width: 0; }
.link-body .lk-title { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.link-body .lk-url   { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.link-body .lk-desc  { font-size: 13px; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.link-actions { display: flex; gap: 4px; flex-shrink: 0; }

/* ── buttons ─────────────────────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 7px 13px;
    border-radius: var(--radius);
    border: 1px solid transparent;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: background .15s, box-shadow .15s, border-color .15s;
    text-decoration: none;
    white-space: nowrap;
    font-family: inherit;
}
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-dk); box-shadow: 0 0 16px var(--glow); }
.btn-ghost   { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--surface2); border-color: var(--accent); }
.btn-icon    { padding: 6px 8px; background: transparent; border-color: transparent; color: var(--muted); }
.btn-icon:hover { background: var(--surface2); color: var(--text); }
.btn-danger  { background: transparent; border-color: transparent; color: var(--danger); }
.btn-danger:hover { background: rgba(247,37,133,.1); }
.btn-sm { padding: 4px 10px; font-size: 12px; }

/* ── modal ───────────────────────────────────────────────────────────── */
.modal-overlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,.55);
    display: flex; align-items: center; justify-content: center;
    z-index: 100;
    opacity: 0;
    transition: opacity .2s;
    pointer-events: none;
}
.modal-overlay.open { opacity: 1; pointer-events: all; }
.modal {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 24px;
    width: min(480px, 94vw);
    box-shadow: 0 8px 40px rgba(0,0,0,.4), 0 0 40px var(--glow);
    transform: translateY(12px);
    transition: transform .2s;
}
.modal-overlay.open .modal { transform: translateY(0); }
.modal h3 { font-size: 17px; margin-bottom: 16px; font-weight: 700; }
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 11px; font-weight: 700; margin-bottom: 5px; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; }
.form-group input,
.form-group textarea {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 9px 12px;
    font-size: 14px;
    font-family: inherit;
    outline: none;
    transition: border-color .15s, box-shadow .15s;
    background: var(--bg);
    color: var(--text);
}
.form-group input:focus,
.form-group textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(155,93,229,.2); }
.form-group textarea { resize: vertical; min-height: 72px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

/* ── save indicator ──────────────────────────────────────────────────── */
#save-indicator {
    font-size: 12px;
    color: var(--muted);
    position: fixed;
    bottom: 16px;
    right: 20px;
    background: var(--surface);
    border: 1px solid var(--border);
    padding: 5px 12px;
    border-radius: 20px;
    opacity: 0;
    transition: opacity .3s;
    pointer-events: none;
    box-shadow: 0 0 12px var(--glow);
}
#save-indicator.show { opacity: 1; }

/* ── blocked / private space ─────────────────────────────────────────── */
#private-block {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: var(--muted);
}
#private-block .pb-icon { font-size: 48px; }
#private-block h2 { font-size: 22px; font-weight: 700; color: var(--text); }
#private-block p  { font-size: 14px; }

/* ── scrollbar ───────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--accent); }
