/* =========================================
   Custom Keyboard Focus Engine
========================================= */
*:focus-visible { outline: none !important; }

.icon-btn:focus-visible,
.text-btn:focus-visible,
.m3-btn:focus-visible,
.menu-item:focus-visible,
.drawer-item:focus-visible,
.close-btn:focus-visible,
.remove-recent-btn:focus-visible,
.m3-select-btn:focus-visible {
    background-color: var(--md-sys-color-surface-container-highest, rgba(0, 104, 116, 0.12));
}

.m3-switch input:focus-visible + .m3-switch-track {
    outline: 2px solid var(--md-sys-color-primary) !important;
    outline-offset: 2px;
}

[data-theme="dark"] .icon-btn:focus-visible,
[data-theme="dark"] .text-btn:focus-visible,
[data-theme="dark"] .m3-btn:focus-visible,
[data-theme="dark"] .menu-item:focus-visible,
[data-theme="dark"] .drawer-item:focus-visible,
[data-theme="dark"] .close-btn:focus-visible,
[data-theme="dark"] .remove-recent-btn:focus-visible,
[data-theme="dark"] .m3-select-btn:focus-visible {
    background-color: rgba(255, 255, 255, 0.12);
}

body { display: flex; }

#workspace {
    flex-grow: 1; display: flex; flex-direction: column;
    height: 100dvh; overflow: hidden;
}

/* =========================================
   Top App Bar & Dropdown
========================================= */
.top-app-bar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 8px 12px; background-color: var(--md-sys-color-surface);
    border-bottom: 1px solid var(--md-sys-color-surface-container-high);
    min-height: 56px; flex-shrink: 0; z-index: 10; gap: 16px;
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

.top-bar-left { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.top-bar-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
#current-file-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
#current-file-name.editable { cursor: text; padding: 4px 8px; border-radius: 4px; transition: background-color 0.2s; }
#current-file-name.editable:hover { background-color: var(--md-sys-color-surface-container-high); }
#current-file-name.editable:focus-visible { background-color: var(--md-sys-color-surface-container-high); }

.icon-btn {
    background: transparent; border: none; color: var(--md-sys-color-on-surface);
    cursor: pointer; border-radius: var(--md-sys-shape-corner-full);
    width: 48px; height: 48px; display: flex; align-items: center; justify-content: center;
    transition: background-color 0.2s, color 0.3s ease;
}
.dropdown-btn { width: auto; padding: 0 8px 0 16px; gap: 2px; }
.icon-btn:hover { background-color: var(--md-sys-color-surface-container-high); }
.icon-btn:disabled { opacity: 0.3; cursor: not-allowed; }

.dropdown-anchor { position: relative; }

.m3-menu {
    position: absolute; top: 56px; right: 0;
    background-color: var(--md-sys-color-surface-container);
    border-radius: var(--md-sys-shape-corner-extra-large);
    box-shadow: 0 8px 24px rgba(0,0,0,0.15);
    min-width: 220px; padding: 12px 0; z-index: 200;
    display: flex; flex-direction: column; opacity: 1;
    visibility: visible;
    transition: opacity 0.2s ease, transform 0.2s ease, background-color 0.3s ease, visibility 0s;
    transform-origin: top right;
}
.m3-menu.hidden { 
    opacity: 0; pointer-events: none; transform: scale(0.95); 
    visibility: hidden;
    transition: opacity 0.2s ease, transform 0.2s ease, background-color 0.3s ease, visibility 0s linear 0.2s;
}

.custom-context-menu {
    position: fixed; background-color: var(--md-sys-color-surface-container);
    border-radius: 12px; box-shadow: 0 4px 16px rgba(0,0,0,0.2);
    min-width: 180px; padding: 8px 0; z-index: 9999;
    display: flex; flex-direction: column; opacity: 0;
    pointer-events: none; transform: scale(0.95);
    transform-origin: top left;
    visibility: hidden;
    transition: opacity 0.1s ease, transform 0.1s ease, background-color 0.3s ease, visibility 0s linear 0.1s;
}
.custom-context-menu.active { 
    opacity: 1; pointer-events: all; transform: scale(1); 
    visibility: visible;
    transition: opacity 0.1s ease, transform 0.1s ease, background-color 0.3s ease, visibility 0s;
}

.menu-item {
    display: flex; align-items: center; gap: 16px; padding: 12px 24px;
    background: transparent; border: none; width: 100%; text-align: left;
    color: var(--md-sys-color-on-surface); font-family: inherit; font-size: var(--md-sys-typescale-body-large-size); cursor: pointer;
    transition: color 0.3s ease, background-color 0.2s ease;
}
.menu-item:hover { background-color: var(--md-sys-color-surface-container-high); }
.menu-item.disabled { opacity: 0.5; cursor: default; }
.menu-divider { height: 1px; background-color: var(--md-sys-color-outline); margin: 8px 0; opacity: 0.2; }
.menu-label { padding: 4px 24px; font-size: var(--md-sys-typescale-label-large-size); color: var(--md-sys-color-outline); text-transform: uppercase; }

/* =========================================
   Sidebar / Drawer
========================================= */
.md-drawer {
    position: fixed; top: 0; left: -300px; width: 300px; height: 100dvh;
    background-color: var(--md-sys-color-surface-container); z-index: 100; display: flex; flex-direction: column;
    transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1), background-color 0.3s ease;
}
.md-drawer.open { left: 0; }
.drawer-header { padding: 24px 28px; flex-shrink: 0; }
.drawer-content { flex-grow: 1; overflow-y: auto; }
.section-label { padding: 0 28px; margin-bottom: 8px; display: block; transition: color 0.3s ease; }
.drawer-list { list-style: none; padding: 0 12px; margin: 0; }

.drawer-item {
    display: flex; align-items: center; justify-content: space-between;
    padding: 10px 16px; margin-bottom: 4px;
    border-radius: var(--md-sys-shape-corner-full);
    cursor: pointer; background: transparent; border: none;
    width: 100%; text-align: left; color: var(--md-sys-color-on-surface); font-family: inherit;
    transition: color 0.3s ease, background-color 0.2s ease;
}
.drawer-item:hover { background-color: rgba(0,0,0,0.05); }
.drawer-item.active { background-color: var(--md-sys-color-primary-container); color: var(--md-sys-color-on-primary-container); }
[data-theme="dark"] .drawer-item:hover { background-color: rgba(255,255,255,0.05); }

.file-info { display: flex; align-items: center; gap: 12px; overflow: hidden; pointer-events: none; }
.file-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; pointer-events: none; }
.close-btn {
    background: transparent; border: none; color: inherit; cursor: pointer;
    opacity: 0.5; padding: 4px; display: flex; border-radius: 50%; align-items: center; justify-content: center;
}
.close-btn:hover { opacity: 1; background-color: rgba(0,0,0,0.1); }
[data-theme="dark"] .close-btn:hover { background-color: rgba(255,255,255,0.1); }
.close-btn .material-symbols-rounded { font-size: 18px; pointer-events: none; }

.drawer-footer { padding: 12px; border-top: 1px solid rgba(0,0,0,0.05); flex-shrink: 0; }
[data-theme="dark"] .drawer-footer { border-top-color: rgba(255,255,255,0.05); }

/* =========================================
   Overlays, Modals & Bottom Sheets
========================================= */
.scrim {
    position: fixed; top: 0; left: 0; width: 100vw; height: 100dvh;
    background-color: rgba(0, 0, 0, 0.4); z-index: 90; display: none;
}
.scrim.active { display: block; }
#modal-overlay, #status-sheet-scrim { z-index: 290; } 

.m3-dialog {
    position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%) scale(0.95);
    background-color: var(--md-sys-color-surface-container-high);
    border-radius: var(--md-sys-shape-corner-extra-large);
    padding: 24px; width: 90%; max-width: 400px; z-index: 300;
    display: flex; flex-direction: column; gap: 24px;
    opacity: 0; pointer-events: none;
    visibility: hidden;
    transition: opacity 0.2s ease, transform 0.2s ease, background-color 0.3s ease, visibility 0s linear 0.2s;
    box-shadow: 0 10px 32px rgba(0,0,0,0.2);
}
.m3-dialog.active { 
    opacity: 1; pointer-events: all; transform: translate(-50%, -50%) scale(1); 
    visibility: visible;
    transition: opacity 0.2s ease, transform 0.2s ease, background-color 0.3s ease, visibility 0s;
}

.m3-dialog h3 { margin: 0; color: var(--md-sys-color-on-surface); transition: color 0.3s ease; }

/* FIXED: Allow modals to scroll safely if content like licenses gets too long */
.dialog-content {
    max-height: 50vh;
    overflow-y: auto;
    padding-right: 8px; /* Room for scrollbar */
}

.m3-text-input {
    width: 100%; padding: 12px 16px; border-radius: 8px;
    border: 1px solid var(--md-sys-color-outline);
    background-color: var(--md-sys-color-surface);
    color: var(--md-sys-color-on-surface); font-family: inherit;
    font-size: var(--md-sys-typescale-body-large-size); outline: none; box-sizing: border-box;
    transition: background-color 0.3s ease, color 0.3s ease, border-color 0.2s;
}
.m3-text-input:focus-visible { border-color: var(--md-sys-color-primary); border-width: 2px; padding: 11px 15px; }

.dialog-actions { display: flex; justify-content: space-between; gap: 8px; margin-top: 8px; }
.m3-btn {
    display: inline-flex; align-items: center; justify-content: center;
    height: 40px; padding: 0 24px; border-radius: 20px; font-family: inherit;
    font-size: var(--md-sys-typescale-label-large-size); font-weight: 500;
    border: none; cursor: pointer; transition: background-color 0.2s, box-shadow 0.2s, color 0.3s ease;
}
.m3-btn-text { background: transparent; color: var(--md-sys-color-primary); }
.m3-btn-text:hover { background-color: rgba(0, 104, 116, 0.08); }
[data-theme="dark"] .m3-btn-text:hover { background-color: rgba(79, 216, 235, 0.08); }

.m3-btn-filled { background-color: var(--md-sys-color-primary); color: var(--md-sys-color-on-primary); }
.m3-btn-filled:hover { background-color: #005a64; box-shadow: 0 1px 3px rgba(0,0,0,0.2); }
[data-theme="dark"] .m3-btn-filled:hover { background-color: #3bbbcc; }

.m3-btn-danger { color: #ba1a1a !important; }
.m3-btn-danger:hover { background-color: rgba(186, 26, 26, 0.08); }
[data-theme="dark"] .m3-btn-danger { color: #ffb4ab !important; }
[data-theme="dark"] .m3-btn-danger:hover { background-color: rgba(255, 180, 171, 0.08); }

.m3-bottom-sheet {
    position: fixed; bottom: 0; left: 0; width: 100%;
    background-color: var(--md-sys-color-surface-container-high);
    border-radius: 28px 28px 0 0; padding: 16px 24px 32px 24px;
    z-index: 300; display: flex; flex-direction: column;
    transform: translateY(100%); 
    transition: transform 0.3s cubic-bezier(0.2, 0, 0, 1), background-color 0.3s ease;
    box-shadow: 0 -8px 24px rgba(0,0,0,0.15);
}
.m3-bottom-sheet.active { transform: translateY(0); }
.sheet-drag-handle {
    width: 32px; height: 4px; border-radius: 2px;
    background-color: var(--md-sys-color-outline); opacity: 0.4;
    margin: 0 auto 16px auto;
}
.sheet-item {
    display: flex; justify-content: space-between; align-items: center;
    padding: 12px 0; border-bottom: 1px solid var(--md-sys-color-surface-container-highest);
    font-size: var(--md-sys-typescale-body-large-size);
}
.sheet-item:last-child { border-bottom: none; }
.sheet-item span:first-child { color: var(--md-sys-color-outline); }

@media (min-width: 900px) {
    #menu-btn { display: none; }
    .md-drawer { position: relative; left: 0; z-index: 1; border-right: 1px solid var(--md-sys-color-surface-container-high); }
    #drawer-overlay.active { display: none; } 
}

/* =========================================
   Workspaces
========================================= */
.editor-canvas { display: none; flex-grow: 1; height: 100%; min-height: 0; }
.editor-canvas.active { display: flex; flex-direction: column; }
#welcome-view, #settings-view { overflow-y: auto; }

#editor-view { overflow: hidden; position: relative; }

.welcome-content { margin: auto; padding: 24px; max-width: 800px; width: 100%; }
.action-columns { display: flex; flex-direction: column; gap: 48px; }
.action-group { display: flex; flex-direction: column; gap: 12px; }
.text-btn { display: flex; align-items: center; gap: 12px; background: transparent; border: none; color: var(--md-sys-color-primary); font-family: inherit; cursor: pointer; padding: 8px 0; width: fit-content; transition: color 0.3s ease, background-color 0.2s ease; }
@media (min-width: 600px) { .action-columns { flex-direction: row; gap: 80px; } }

#editor-container {
    display: flex; flex-direction: row; flex-grow: 1; width: 100%; overflow: hidden;
}
#editor-container.hidden { display: none !important; }

#line-numbers-gutter {
    padding: 24px 16px;
    background-color: var(--md-sys-color-surface-container);
    color: var(--md-sys-color-outline);
    font-family: var(--editor-font-family);
    font-size: var(--md-sys-typescale-body-large-size);
    line-height: 1.5; 
    text-align: right;
    overflow-y: hidden;
    user-select: none;
    border-right: 1px solid var(--md-sys-color-surface-container-high);
    min-width: 48px;
    flex-shrink: 0;
    white-space: pre;
    transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
}

#line-numbers-gutter.hidden { display: none !important; }

#main-editor {
    flex-grow: 1; height: 100%; width: 100%; border: none; resize: none; outline: none; padding: 24px;
    font-family: var(--editor-font-family);
    font-size: var(--md-sys-typescale-body-large-size);
    line-height: 1.5;
    background: transparent; color: var(--md-sys-color-on-surface); overflow-y: auto; box-sizing: border-box; transition: color 0.3s ease;
    white-space: pre-wrap; 
}

#main-editor::placeholder { font-family: inherit; opacity: 0.6; }

/* FIXED: Reverted headings to neutral document styling, repurposed primary color for inline code */
#markdown-preview {
    flex-grow: 1; height: 100%; width: 100%; padding: 32px 24px;
    overflow-y: auto; box-sizing: border-box;
    color: var(--md-sys-color-on-surface);
    background-color: transparent;
    font-family: var(--md-sys-typescale-display-large-font);
    line-height: 1.6;
}
#markdown-preview.hidden { display: none !important; }

#markdown-preview-content {
    max-width: 800px;
    margin: 0 auto;
}
#markdown-preview-content h1, 
#markdown-preview-content h2, 
#markdown-preview-content h3, 
#markdown-preview-content h4 {
    color: var(--md-sys-color-on-surface); /* Neutral Document Feel */
    margin-top: 32px; margin-bottom: 16px; font-weight: 600;
}
#markdown-preview-content h1 { font-size: 36px; border-bottom: 1px solid var(--md-sys-color-surface-container-highest); padding-bottom: 8px; }
#markdown-preview-content h2 { font-size: 28px; }
#markdown-preview-content p { margin-bottom: 16px; }

/* NEW: Beautiful Inline Code highlighting using Primary Brand Accent */
#markdown-preview-content code { 
    font-family: var(--editor-font-family);
    background-color: var(--md-sys-color-surface-container-high);
    color: var(--md-sys-color-primary); 
    padding: 3px 6px; 
    border-radius: 6px; 
    font-size: 0.85em;
    border: 1px solid var(--md-sys-color-surface-container-highest);
}

/* Pre block distinct styling */
#markdown-preview-content pre {
    background-color: var(--md-sys-color-surface-container-highest);
    padding: 16px; border-radius: 12px; overflow-x: auto;
    margin-bottom: 16px;
    border: 1px solid var(--md-sys-color-surface-container-high);
}
#markdown-preview-content pre code { 
    background-color: transparent; 
    padding: 0; 
    color: var(--md-sys-color-on-surface); 
    border: none;
}

#markdown-preview-content blockquote {
    border-left: 4px solid var(--md-sys-color-primary);
    margin: 0 0 16px 0; padding-left: 16px; color: var(--md-sys-color-outline);
    font-style: italic;
}
#markdown-preview-content a { color: var(--md-sys-color-primary); text-decoration: none; }
#markdown-preview-content a:hover { text-decoration: underline; }
#markdown-preview-content img { max-width: 100%; border-radius: 12px; margin-bottom: 16px; }
#markdown-preview-content ul, 
#markdown-preview-content ol { margin-bottom: 16px; padding-left: 24px; }

/* NEW: Table Support */
#markdown-preview-content table {
    border-collapse: collapse;
    width: 100%;
    margin-bottom: 16px;
}
#markdown-preview-content th, 
#markdown-preview-content td {
    border: 1px solid var(--md-sys-color-surface-container-highest);
    padding: 8px 12px;
}
#markdown-preview-content th {
    background-color: var(--md-sys-color-surface-container-high);
}


#status-bar {
    display: flex; align-items: center; justify-content: space-between;
    height: 28px; padding: 0 16px; flex-shrink: 0;
    background-color: var(--md-sys-color-surface-container);
    border-top: 1px solid var(--md-sys-color-surface-container-high);
    color: var(--md-sys-color-on-surface);
    font-size: 12px; z-index: 10;
    user-select: none; 
    transition: all 0.3s cubic-bezier(0.2, 0, 0, 1);
    overflow: hidden;
}

#status-bar.pill-mode {
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    width: fit-content;
    height: 40px;
    border-radius: var(--md-sys-shape-corner-full);
    background-color: var(--md-sys-color-primary); 
    color: var(--md-sys-color-on-primary); 
    border-top: none;
    box-shadow: none;
    gap: 8px; 
    padding: 0 20px;
}

.status-left, .status-right { display: flex; gap: 24px; white-space: nowrap; opacity: 0.8; }
.status-item { cursor: default; transition: opacity 0.2s ease; }
.status-item:hover { opacity: 1; }

#status-bar.pill-mode .status-left, 
#status-bar.pill-mode .status-right {
    opacity: 1;
}

.recent-item-wrapper { display: flex; align-items: center; justify-content: space-between; width: 100%; max-width: 320px; }
.dropdown-recent-item-wrapper { display: flex; align-items: center; justify-content: space-between; padding-right: 8px; }
.dropdown-recent-item-wrapper .menu-item { flex-grow: 1; width: auto; padding-right: 8px; }
.remove-recent-btn { width: 32px; height: 32px; opacity: 0.4; transition: opacity 0.2s, background-color 0.2s; }
.remove-recent-btn:hover { opacity: 1; background-color: var(--md-sys-color-surface-container-high); }

/* Settings View Styles */
.settings-content {
    padding: 32px 24px; max-width: 800px; margin: 0 auto; width: 100%;
    display: flex; flex-direction: column; gap: 24px;
}
.settings-card {
    background-color: var(--md-sys-color-surface-container);
    border-radius: var(--md-sys-shape-corner-extra-large); padding: 24px;
    display: flex; flex-direction: column; transition: background-color 0.3s ease;
}
.settings-card-title {
    color: var(--md-sys-color-primary); margin: 0 0 24px 0; transition: color 0.3s ease;
}
.setting-item {
    display: flex; justify-content: space-between; align-items: center;
    padding-bottom: 24px; margin-bottom: 24px; border-bottom: 1px solid var(--md-sys-color-surface-container-high);
    transition: border-color 0.3s ease;
}
.setting-item:last-child { border-bottom: none; padding-bottom: 0; margin-bottom: 0; }

.setting-info { 
    display: flex; flex-direction: column; gap: 4px; 
    flex: 1; 
    padding-right: 16px; 
}
.m3-switch, .dropdown-anchor {
    flex-shrink: 0;
}

.m3-select-btn {
    display: flex; align-items: center; justify-content: space-between;
    padding: 12px 16px; border-radius: 8px; border: none;
    background-color: var(--md-sys-color-surface-container-high);
    color: var(--md-sys-color-on-surface); font-family: inherit;
    font-size: var(--md-sys-typescale-body-large-size); cursor: pointer;
    min-width: 180px; transition: background-color 0.2s ease, color 0.3s ease;
}
.m3-select-btn:hover { background-color: rgba(0, 0, 0, 0.05); }
[data-theme="dark"] .m3-select-btn:hover { background-color: rgba(255, 255, 255, 0.05); }

.m3-switch { position: relative; display: inline-block; width: 52px; height: 32px; }
.m3-switch input { opacity: 0; width: 0; height: 0; }
.m3-switch-track {
    position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0;
    background-color: var(--md-sys-color-surface-container-highest, #e3e3e3);
    border: 2px solid var(--md-sys-color-outline);
    border-radius: 16px; transition: .3s, background-color 0.3s ease;
}
[data-theme="dark"] .m3-switch-track { background-color: var(--md-sys-color-surface-container-high); }

.m3-switch-thumb {
    position: absolute; content: ""; height: 16px; width: 16px; left: 6px; bottom: 6px;
    background-color: var(--md-sys-color-outline);
    border-radius: 50%; transition: .3s;
}
.m3-switch input:checked + .m3-switch-track { background-color: var(--md-sys-color-primary); border-color: var(--md-sys-color-primary); }
.m3-switch input:checked + .m3-switch-track .m3-switch-thumb {
    transform: translateX(20px); background-color: var(--md-sys-color-on-primary);
    height: 24px; width: 24px; bottom: 2px; left: 4px;
}