/* ========================================
   CRDT Notepad — Premium Dark Theme
   ======================================== */

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

/* ── Design Tokens ── */
:root {
    --primary: #10B981;
    --primary-rgb: 16, 185, 129;
    --primary-hover: #059669;
    --primary-glow: rgba(16, 185, 129, 0.25);
    --accent: #6366F1;
    --accent-rgb: 99, 102, 241;
    --accent-hover: #4F46E5;
    --accent-glow: rgba(99, 102, 241, 0.25);

    --bg-deep: #0F172A;
    --bg-base: #1E293B;
    --bg-surface: #334155;
    --bg-elevated: #475569;
    --bg-glass: rgba(30, 41, 59, 0.75);
    --bg-glass-hover: rgba(51, 65, 85, 0.85);

    --text-primary: #F1F5F9;
    --text-secondary: #94A3B8;
    --text-muted: #64748B;
    --text-inverse: #0F172A;

    --border: rgba(148, 163, 184, 0.12);
    --border-hover: rgba(148, 163, 184, 0.25);
    --border-focus: var(--primary);

    --danger: #EF4444;
    --warning: #F59E0B;
    --success: #10B981;

    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 20px;
    --radius-full: 9999px;

    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.5);
    --shadow-glow: 0 0 20px var(--primary-glow);

    --font-sans: 'Inter', system-ui, -apple-system, sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', monospace;

    --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-base: 250ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-slow: 400ms cubic-bezier(0.4, 0, 0.2, 1);
}

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

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: var(--font-sans);
    background: var(--bg-deep);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
    overflow: hidden;
}

/* ── App Layout ── */
.app {
    display: grid;
    grid-template-rows: auto 1fr;
    height: 100vh;
    width: 100vw;
}

/* ── Header ── */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    background: var(--bg-glass);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-bottom: 1px solid var(--border);
    z-index: 100;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 16px;
}

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    font-size: 1.15rem;
    color: var(--text-primary);
    text-decoration: none;
}

.logo-icon {
    width: 32px;
    height: 32px;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    box-shadow: 0 2px 8px var(--primary-glow);
}

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

.header-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

/* ── Connection Status ── */
.connection-status {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 12px;
    border-radius: var(--radius-full);
    font-size: 0.75rem;
    font-weight: 500;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    transition: all var(--transition-base);
}

.connection-status.connected {
    background: rgba(16, 185, 129, 0.1);
    border-color: rgba(16, 185, 129, 0.3);
    color: var(--primary);
}

.connection-status.disconnected {
    color: var(--text-muted);
}

.connection-status.connecting {
    color: var(--warning);
    background: rgba(245, 158, 11, 0.1);
    border-color: rgba(245, 158, 11, 0.3);
}

.status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
}

.connection-status.connected .status-dot {
    animation: pulse-dot 2s ease-in-out infinite;
}

@keyframes pulse-dot {

    0%,
    100% {
        opacity: 1;
        box-shadow: 0 0 0 0 var(--primary-glow);
    }

    50% {
        opacity: 0.7;
        box-shadow: 0 0 0 4px transparent;
    }
}

/* ── Peer Count Badge ── */
.peer-count {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.75rem;
    color: var(--text-muted);
    padding: 4px 8px;
    border-radius: var(--radius-full);
    background: var(--bg-surface);
}

.peer-count svg {
    width: 14px;
    height: 14px;
}

/* ── Buttons ── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 6px 14px;
    border-radius: var(--radius-sm);
    font-family: var(--font-sans);
    font-size: 0.8rem;
    font-weight: 500;
    border: 1px solid var(--border);
    background: var(--bg-surface);
    color: var(--text-primary);
    cursor: pointer;
    transition: all var(--transition-fast);
    white-space: nowrap;
}

.btn:hover {
    background: var(--bg-glass-hover);
    border-color: var(--border-hover);
    transform: translateY(-1px);
}

.btn:active {
    transform: translateY(0);
}

.btn-primary {
    background: var(--primary);
    color: var(--text-inverse);
    border-color: var(--primary);
}

.btn-primary:hover {
    background: var(--primary-hover);
    border-color: var(--primary-hover);
    box-shadow: 0 4px 12px var(--primary-glow);
}

.btn-accent {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

.btn-accent:hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
    box-shadow: 0 4px 12px var(--accent-glow);
}

.btn-icon {
    padding: 6px;
    width: 32px;
    height: 32px;
}

.btn-icon svg {
    width: 16px;
    height: 16px;
}

.btn-danger {
    color: var(--danger);
}

.btn-danger:hover {
    background: rgba(239, 68, 68, 0.15);
    border-color: rgba(239, 68, 68, 0.3);
}

/* ── Main Layout ── */
.main-area {
    display: grid;
    grid-template-columns: 260px 1fr;
    overflow: hidden;
}

/* ── Sidebar ── */
.sidebar {
    background: var(--bg-base);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: width var(--transition-base);
}

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

.sidebar-title {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.doc-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.doc-list::-webkit-scrollbar {
    width: 4px;
}

.doc-list::-webkit-scrollbar-thumb {
    background: var(--bg-elevated);
    border-radius: var(--radius-full);
}

.doc-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition-fast);
    border: 1px solid transparent;
    margin-bottom: 2px;
}

.doc-item:hover {
    background: var(--bg-surface);
}

.doc-item.active {
    background: rgba(16, 185, 129, 0.08);
    border-color: rgba(16, 185, 129, 0.2);
}

.doc-item-icon {
    font-size: 1.1rem;
    flex-shrink: 0;
}

.doc-item-info {
    flex: 1;
    min-width: 0;
}

.doc-item-name {
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.doc-item-meta {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 2px;
}

.doc-item-delete {
    opacity: 0;
    transition: opacity var(--transition-fast);
    padding: 2px;
    border: none;
    background: none;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: var(--radius-sm);
}

.doc-item:hover .doc-item-delete {
    opacity: 1;
}

.doc-item-delete:hover {
    color: var(--danger);
    background: rgba(239, 68, 68, 0.1);
}

/* ── Editor Area ── */
.editor-area {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* ── Toolbar ── */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    background: var(--bg-base);
    border-bottom: 1px solid var(--border);
    gap: 8px;
}

.toolbar-left {
    display: flex;
    align-items: center;
    gap: 4px;
}

.toolbar-center {
    display: flex;
    align-items: center;
    gap: 2px;
}

.toolbar-right {
    display: flex;
    align-items: center;
    gap: 8px;
}

.view-toggle {
    display: flex;
    align-items: center;
    background: var(--bg-surface);
    border-radius: var(--radius-sm);
    padding: 2px;
    border: 1px solid var(--border);
}

.view-toggle .btn {
    border: none;
    background: transparent;
    padding: 4px 10px;
    font-size: 0.75rem;
    border-radius: 6px;
}

.view-toggle .btn.active {
    background: var(--primary);
    color: var(--text-inverse);
}

.doc-title-input {
    font-family: var(--font-sans);
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-primary);
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    padding: 4px 10px;
    outline: none;
    transition: all var(--transition-fast);
    min-width: 200px;
}

.doc-title-input:hover {
    border-color: var(--border);
}

.doc-title-input:focus {
    border-color: var(--primary);
    background: var(--bg-surface);
}

/* ── Editor Content ── */
.editor-content {
    flex: 1;
    display: grid;
    overflow: hidden;
}

.editor-content.split-view {
    grid-template-columns: 1fr 1fr;
}

.editor-pane {
    position: relative;
    overflow: hidden;
}

.editor-textarea {
    width: 100%;
    height: 100%;
    padding: 24px 32px;
    font-family: var(--font-mono);
    font-size: 0.9rem;
    line-height: 1.75;
    color: var(--text-primary);
    background: var(--bg-deep);
    border: none;
    resize: none;
    outline: none;
    tab-size: 2;
}

.editor-textarea::placeholder {
    color: var(--text-muted);
}

.editor-textarea::-webkit-scrollbar {
    width: 6px;
}

.editor-textarea::-webkit-scrollbar-thumb {
    background: var(--bg-elevated);
    border-radius: var(--radius-full);
}

.editor-textarea::-webkit-scrollbar-track {
    background: transparent;
}

/* ── Markdown Preview ── */
.preview-pane {
    overflow-y: auto;
    padding: 24px 32px;
    background: var(--bg-deep);
    border-left: 1px solid var(--border);
}

.preview-pane::-webkit-scrollbar {
    width: 6px;
}

.preview-pane::-webkit-scrollbar-thumb {
    background: var(--bg-elevated);
    border-radius: var(--radius-full);
}

.preview-content h1 {
    font-size: 1.8rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 16px;
    padding-bottom: 8px;
    border-bottom: 2px solid var(--border);
}

.preview-content h2 {
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--text-primary);
    margin: 24px 0 12px;
}

.preview-content h3 {
    font-size: 1.15rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin: 20px 0 10px;
}

.preview-content p {
    margin-bottom: 12px;
    color: var(--text-secondary);
}

.preview-content code {
    font-family: var(--font-mono);
    font-size: 0.85em;
    background: var(--bg-surface);
    padding: 2px 6px;
    border-radius: 4px;
    color: var(--primary);
}

.preview-content pre {
    background: var(--bg-base);
    padding: 16px 20px;
    border-radius: var(--radius-md);
    overflow-x: auto;
    margin: 16px 0;
    border: 1px solid var(--border);
}

.preview-content pre code {
    background: none;
    padding: 0;
    color: var(--text-primary);
}

.preview-content blockquote {
    border-left: 3px solid var(--accent);
    padding: 8px 16px;
    margin: 12px 0;
    color: var(--text-muted);
    background: rgba(99, 102, 241, 0.05);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.preview-content ul,
.preview-content ol {
    margin: 12px 0;
    padding-left: 24px;
    color: var(--text-secondary);
}

.preview-content li {
    margin-bottom: 6px;
}

.preview-content a {
    color: var(--primary);
    text-decoration: none;
}

.preview-content a:hover {
    text-decoration: underline;
}

.preview-content hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 24px 0;
}

.preview-content strong {
    color: var(--text-primary);
    font-weight: 600;
}

.preview-content em {
    color: var(--text-secondary);
}

.preview-content img {
    max-width: 100%;
    border-radius: var(--radius-md);
}

.preview-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 16px 0;
}

.preview-content th,
.preview-content td {
    border: 1px solid var(--border);
    padding: 8px 12px;
    text-align: left;
}

.preview-content th {
    background: var(--bg-surface);
    font-weight: 600;
}

/* ── Status Bar ── */
.status-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 16px;
    background: var(--bg-base);
    border-top: 1px solid var(--border);
    font-size: 0.7rem;
    color: var(--text-muted);
}

.status-bar-left,
.status-bar-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.status-item {
    display: flex;
    align-items: center;
    gap: 4px;
}

.save-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
}

.save-indicator.saving {
    color: var(--warning);
}

.save-indicator.saved {
    color: var(--primary);
}

/* ── Modals ── */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-base);
}

.modal-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.modal {
    background: var(--bg-base);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 24px;
    width: 90%;
    max-width: 440px;
    box-shadow: var(--shadow-lg);
    transform: translateY(20px) scale(0.95);
    transition: transform var(--transition-base);
}

.modal-overlay.active .modal {
    transform: translateY(0) scale(1);
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}

.modal-title {
    font-size: 1.1rem;
    font-weight: 600;
}

.modal-body {
    margin-bottom: 20px;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

/* ── Form Inputs ── */
.input {
    width: 100%;
    padding: 8px 12px;
    font-family: var(--font-sans);
    font-size: 0.85rem;
    color: var(--text-primary);
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    outline: none;
    transition: border-color var(--transition-fast);
}

.input:focus {
    border-color: var(--primary);
}

.input-label {
    display: block;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-muted);
    margin-bottom: 6px;
}

.share-code-display {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 12px;
}

.share-code {
    flex: 1;
    padding: 10px 14px;
    font-family: var(--font-mono);
    font-size: 1.1rem;
    font-weight: 600;
    letter-spacing: 0.15em;
    color: var(--primary);
    background: var(--bg-surface);
    border: 1px solid rgba(16, 185, 129, 0.3);
    border-radius: var(--radius-sm);
    text-align: center;
}

/* ── History Panel ── */
.history-panel {
    position: fixed;
    top: 0;
    right: -400px;
    bottom: 0;
    width: 380px;
    background: var(--bg-base);
    border-left: 1px solid var(--border);
    z-index: 200;
    display: flex;
    flex-direction: column;
    transition: right var(--transition-slow);
    box-shadow: var(--shadow-lg);
}

.history-panel.open {
    right: 0;
}

.history-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}

.history-list {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
}

.history-entry {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    margin-bottom: 4px;
    transition: background var(--transition-fast);
}

.history-entry:hover {
    background: var(--bg-surface);
}

.history-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-top: 6px;
    flex-shrink: 0;
}

.history-dot.insert {
    background: var(--primary);
}

.history-dot.delete {
    background: var(--danger);
}

.history-info {
    flex: 1;
}

.history-action {
    font-size: 0.8rem;
    color: var(--text-primary);
}

.history-time {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 2px;
}

/* ── Toast Notifications ── */
.toast-container {
    position: fixed;
    bottom: 40px;
    right: 20px;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.toast {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    background: var(--bg-glass);
    backdrop-filter: blur(16px);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-md);
    font-size: 0.85rem;
    color: var(--text-primary);
    animation: toast-in 0.3s ease-out;
    max-width: 340px;
}

.toast.success {
    border-left: 3px solid var(--primary);
}

.toast.error {
    border-left: 3px solid var(--danger);
}

.toast.info {
    border-left: 3px solid var(--accent);
}

@keyframes toast-in {
    from {
        opacity: 0;
        transform: translateX(40px);
    }

    to {
        opacity: 1;
        transform: translateX(0);
    }
}

@keyframes toast-out {
    from {
        opacity: 1;
        transform: translateX(0);
    }

    to {
        opacity: 0;
        transform: translateX(40px);
    }
}

/* ── Empty State ── */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 16px;
    color: var(--text-muted);
}

.empty-state-icon {
    font-size: 3rem;
    opacity: 0.3;
}

.empty-state-text {
    font-size: 1rem;
    font-weight: 500;
}

.empty-state-hint {
    font-size: 0.8rem;
    color: var(--text-muted);
}

/* ── Responsive ── */
@media (max-width: 900px) {
    .main-area {
        grid-template-columns: 1fr;
    }

    .sidebar {
        position: fixed;
        left: -280px;
        top: 0;
        bottom: 0;
        width: 260px;
        z-index: 300;
        transition: left var(--transition-slow);
        box-shadow: var(--shadow-lg);
    }

    .sidebar.open {
        left: 0;
    }

    .sidebar-overlay {
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 250;
        opacity: 0;
        pointer-events: none;
        transition: opacity var(--transition-base);
    }

    .sidebar-overlay.active {
        opacity: 1;
        pointer-events: auto;
    }

    .header {
        padding: 10px 12px;
    }

    .logo span {
        display: none;
    }

    .editor-content.split-view {
        grid-template-columns: 1fr;
        grid-template-rows: 1fr 1fr;
    }

    .preview-pane {
        border-left: none;
        border-top: 1px solid var(--border);
    }
}

@media (max-width: 600px) {
    .editor-textarea {
        padding: 16px;
        font-size: 0.85rem;
    }

    .preview-pane {
        padding: 16px;
    }

    .toolbar {
        padding: 6px 10px;
    }

    .toolbar .btn span {
        display: none;
    }

    .header-center {
        display: none;
    }
}

/* ── Animations ── */
@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.fade-in {
    animation: fadeIn var(--transition-base) ease-out;
}

.slide-up {
    animation: slideUp var(--transition-base) ease-out;
}

/* ── Keyboard Shortcut Hints ── */
.kbd {
    display: inline-block;
    padding: 2px 5px;
    font-family: var(--font-mono);
    font-size: 0.65rem;
    color: var(--text-muted);
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 4px;
    line-height: 1;
}