:root {
    --accent-color: #8b5cf6;
    --accent-hover: #7c3aed;
    --glass-blur: 12px;
}

body {
    --bg-color: #0f172a;
    --pane-bg: rgba(30, 41, 59, 0.7);
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --border-color: rgba(255, 255, 255, 0.1);
    --header-bg: rgba(15, 23, 42, 0.8);
    --grad-start: rgba(139, 92, 246, 0.15);
    --grad-end: rgba(59, 130, 246, 0.15);
    --tab-bg: rgba(0, 0, 0, 0.2);
    --pane-header-bg: rgba(0, 0, 0, 0.2);
    --code-bg: rgba(255, 255, 255, 0.1);
    --pre-bg: rgba(15, 23, 42, 0.5);
    --alert-bg: rgba(255, 255, 255, 0.03);
    --details-bg: rgba(255, 255, 255, 0.02);
}

body.light-mode {
    --bg-color: #f1f5f9;
    --pane-bg: rgba(255, 255, 255, 0.7);
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --border-color: rgba(0, 0, 0, 0.08);
    --header-bg: rgba(255, 255, 255, 0.8);
    --grad-start: rgba(139, 92, 246, 0.08);
    --grad-end: rgba(59, 130, 246, 0.08);
    --tab-bg: rgba(0, 0, 0, 0.04);
    --pane-header-bg: rgba(0, 0, 0, 0.03);
    --code-bg: rgba(0, 0, 0, 0.06);
    --pre-bg: rgba(255, 255, 255, 0.5);
    --alert-bg: rgba(0, 0, 0, 0.02);
    --details-bg: rgba(0, 0, 0, 0.01);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
    background-image:
        radial-gradient(at 0% 0%, var(--grad-start) 0px, transparent 50%),
        radial-gradient(at 100% 100%, var(--grad-end) 0px, transparent 50%);
    transition: background-color 0.3s, color 0.3s;
}

.app-container {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.workspace-layout {
    display: flex;
    flex: 1;
    overflow: hidden;
    position: relative;
}

.main-content {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
    position: relative;
}

/* --- File Explorer (Sidebar) --- */
.sidebar {
    width: 250px;
    min-width: 150px;
    max-width: 500px;
    background: var(--pane-bg);
    backdrop-filter: blur(var(--glass-blur));
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex: 0 0 auto;
    transition: margin-left 0.3s ease;
    /* For toggle animation */
}

.sidebar.hidden {
    display: none;
    /* simple toggle */
}

.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
    border-bottom: 1px solid var(--border-color);
    background: var(--pane-header-bg);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.file-tree {
    list-style: none;
    padding: 0.5rem 0;
    margin: 0;
    overflow-y: auto;
    flex: 1;
    font-size: 0.85rem;
}

.file-tree li {
    position: relative;
}

.tree-item {
    display: flex;
    align-items: center;
    padding: 4px 8px 4px 1rem;
    cursor: pointer;
    color: var(--text-primary);
    user-select: none;
    transition: background 0.1s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.tree-item:hover {
    background: rgba(139, 92, 246, 0.15);
}

.tree-item.active {
    background: rgba(139, 92, 246, 0.25);
    color: var(--accent-color);
    border-right: 3px solid var(--accent-color);
}

.tree-icon {
    margin-right: 6px;
    font-size: 1rem;
    display: inline-block;
    width: 16px;
    text-align: center;
}

.tree-children {
    list-style: none;
    padding: 0;
    margin: 0;
    display: none;
}

.tree-children.expanded {
    display: block;
}

/* Level Indentation */
.tree-children .tree-item {
    padding-left: 2rem;
}

.tree-children .tree-children .tree-item {
    padding-left: 3rem;
}

.tree-children .tree-children .tree-children .tree-item {
    padding-left: 4rem;
}

.tree-children .tree-children .tree-children .tree-children .tree-item {
    padding-left: 5rem;
}


.pane-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1.25rem;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    border-bottom: 1px solid var(--border-color);
    background: var(--pane-header-bg);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.pane-actions {
    display: flex;
    gap: 0.5rem;
}

.mini-btn {
    background: rgba(139, 92, 246, 0.1) !important;
    border: 1px solid rgba(139, 92, 246, 0.3) !important;
    font-size: 0.7rem !important;
    padding: 4px 10px !important;
    border-radius: 4px;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s;
}

.mini-btn:hover {
    background: rgba(139, 92, 246, 0.2) !important;
    border-color: var(--accent-color) !important;
}

header {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 2rem;
    background: var(--header-bg);
    backdrop-filter: blur(var(--glass-blur));
    border-bottom: 1px solid var(--border-color);
    z-index: 10;
}

header h1 {
    font-size: 1.5rem;
    font-weight: 600;
    background: linear-gradient(135deg, #fff 0%, var(--accent-color) 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.file-operations {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding-right: 1rem;
    border-right: 1px solid var(--border-color);
}

#current-filename {
    font-size: 0.85rem;
    color: var(--text-secondary);
    max-width: 150px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-left: 0.5rem;
    font-weight: 500;
}

/* Context Menu */
.context-menu {
    position: absolute;
    background: var(--pane-bg);
    backdrop-filter: blur(var(--glass_blur));
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
    z-index: 1000;
    min-width: 160px;
    padding: 6px 0;
}

.context-menu ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.context-menu li {
    padding: 8px 16px;
    font-size: 0.9rem;
    cursor: pointer;
    color: var(--text-primary);
    transition: background 0.2s;
}

.context-menu li:hover {
    background: var(--accent-color);
    color: white;
}

.menu-separator {
    height: 1px;
    background: var(--border-color);
    margin: 4px 0 !important;
    padding: 0 !important;
    cursor: default !important;
}

.menu-separator:hover {
    background: var(--border-color) !important;
}

#sync-status {
    font-size: 0.8rem;
    color: var(--text-secondary);
    background: rgba(139, 92, 246, 0.1);
    padding: 4px 12px;
    border-radius: 20px;
    border: 1px solid rgba(139, 92, 246, 0.2);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.toggle-group {
    display: flex;
    gap: 12px;
    align-items: center;
    background: rgba(0, 0, 0, 0.2);
    padding: 2px 10px;
    border-radius: 20px;
    border: 1px solid var(--border-color);
}

.toggle-switch {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    user-select: none;
}

.toggle-switch input {
    display: none;
}

.slider {
    position: relative;
    display: inline-block;
    width: 28px;
    height: 16px;
    background-color: rgba(255, 255, 255, 0.1);
    transition: .3s;
    border-radius: 16px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 12px;
    width: 12px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    transition: .3s;
    border-radius: 50%;
}

input:checked+.slider {
    background-color: var(--accent-color);
}

input:checked+.slider:before {
    transform: translateX(12px);
}

.label-text {
    font-size: 0.75rem;
    color: var(--text-secondary);
    font-weight: 500;
}

.header-actions button {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
    font-family: 'Outfit', sans-serif;
}

.header-actions button:hover:not(:disabled) {
    background: rgba(139, 92, 246, 0.2);
    border-color: var(--accent-color);
}

.header-actions button:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.header-actions button.hidden {
    display: none !important;
}

/* View Modes */
#tab-edit.view-editor-only .preview-pane,
#tab-edit.view-editor-only #pane-resizer {
    display: none !important;
}

#tab-edit.view-editor-only .editor-pane {
    max-width: 100% !important;
    width: 100% !important;
    flex: 1;
}

#tab-edit.view-preview-only .editor-pane,
#tab-edit.view-preview-only #pane-resizer {
    display: none !important;
}

#tab-edit.view-preview-only .preview-pane {
    max-width: 100% !important;
    width: 100% !important;
    flex: 1;
}

/* View Mode Button Highlight */
.view-modes .mini-btn.active {
    background: rgba(139, 92, 246, 0.4) !important;
    border-color: var(--accent-color);
}


.editor-pane,
.preview-pane {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--pane-bg);
    backdrop-filter: blur(var(--glass-blur));
    border-radius: 16px;
    border: 1px solid var(--border-color);
    overflow: hidden;
    transition: transform 0.3s ease;
}

.app-tabs {
    display: flex;
    background: var(--tab-bg);
    padding: 0 1rem;
    border-bottom: 1px solid var(--border-color);
}

.tab-item {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    padding: 0.75rem 1.5rem;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    border-bottom: 2px solid transparent;
    font-family: 'Outfit', sans-serif;
}

.tab-item:hover {
    color: var(--text-primary);
}

.tab-item.active {
    color: var(--accent-color);
    border-bottom-color: var(--accent-color);
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: flex;
    flex: 1;
    overflow: hidden;
    padding: 0;
    /* Remove padding for smoother resizing */
    gap: 0;
    /* Remove gap to use resizer instead */
}

/* Resizer Style */
.resizer {
    width: 8px;
    /* Slightly wider click area */
    flex: 0 0 8px;
    /* Prevent shrinking */
    background: transparent;
    cursor: col-resize;
    transition: background 0.2s;
    z-index: 10;
    position: relative;
    margin: 0 -1px;
    /* Overlap borders slightly if needed */
}

.resizer::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 2px;
    height: 30px;
    background: var(--border-color);
    border-radius: 2px;
    opacity: 0.3;
}

.resizer:hover,
.resizer.active {
    background: rgba(139, 92, 246, 0.2);
}

.resizer:hover::after,
.resizer.active::after {
    background: var(--accent-color);
    opacity: 1;
}

body.resizing {
    cursor: col-resize;
    user-select: none;
}

body.resizing * {
    pointer-events: none !important;
}

.editor-pane {
    width: 50%;
    min-width: 150px;
    /* Reduced minimum width */
    max-width: 85%;
    display: flex;
    flex-direction: column;
    flex: 0 0 auto;
    overflow: hidden;
    /* Added to keep content inside */
}

.preview-pane {
    flex: 1 1 auto;
    /* Take up remaining space */
    min-width: 150px;
    /* Reduced minimum width */
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* Added to keep content inside */
}

.editor-wrapper {
    flex: 1;
    display: flex;
    overflow: hidden;
    position: relative;
    background: transparent;
    width: 100%;
    /* Ensure it doesn't push bounds */
}

.line-numbers {
    width: 3.5rem;
    padding: 1.5rem 0.5rem;
    text-align: right;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.95rem;
    /* Matched with textarea */
    color: var(--text-secondary);
    background: rgba(0, 0, 0, 0.1);
    border-right: 1px solid var(--border-color);
    user-select: none;
    overflow: hidden;
    line-height: 1.6;
}

textarea {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-primary);
    padding: 1.5rem;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.95rem;
    /* Matched with line-numbers */
    line-height: 1.6;
    resize: none;
    white-space: pre;
    /* Prevent wrapping to keep lines perfectly aligned with gutter */
    overflow-x: auto;
    overflow-y: auto;
}

#preview-output {
    flex: 1;
}

/* Preview Layout & TOC */
.preview-layout {
    display: flex;
    flex: 1;
    /* Match height of container */
    position: relative;
    overflow: hidden;
}

.markdown-body {
    flex: 1;
    padding: 2rem;
    overflow-y: auto;
    line-height: 1.6;
    scroll-behavior: smooth;
    position: relative;
    /* Contain scrollIntoView */
    color: var(--text-primary);
    word-wrap: break-word;
}

.markdown-body img {
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.toc-sidebar {
    width: 220px;
    background: rgba(15, 23, 42, 0.3);
    border-left: 1px solid var(--border-color);
    padding: 1rem;
    overflow-y: auto;
    font-size: 0.85rem;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    opacity: 1;
}

.toc-sidebar.hidden {
    width: 0;
    padding: 0;
    border-left: 0;
    opacity: 0;
    pointer-events: none;
}

.toc-header {
    font-weight: 600;
    color: var(--accent-color);
    margin-bottom: 0.75rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

#toc-content ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

#toc-content li {
    margin-bottom: 0.4rem;
}

#toc-content a {
    color: var(--text-secondary);
    text-decoration: none;
    transition: all 0.2s;
    display: block;
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#toc-content a:hover {
    color: var(--accent-color);
    padding-left: 4px;
}

.toc-h1 {
    font-weight: 600;
    font-size: 0.85rem;
    margin-top: 0.5rem;
    color: var(--text-primary) !important;
}

.toc-h2 {
    padding-left: 0.75rem;
}

.toc-h3 {
    padding-left: 1.5rem;
    color: #94a3b8 !important;
    font-size: 0.8rem;
}

/* Markdown Styles */
.markdown-body h1,
.markdown-body h2,
.markdown-body h3 {
    margin-top: 1.25rem;
    margin-bottom: 0.75rem;
}


.markdown-body hr {
    height: 1px;
    padding: 0;
    margin: 2.5rem 0;
    background-color: var(--border-color);
    border: 0;
}

/* 見出し下線の切り替え用 */
.markdown-body.show-underline h1,
.markdown-body.show-underline h2 {
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.3rem;
}

.markdown-body table {
    width: 100%;
    border-collapse: collapse;
    margin: 1rem 0;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 8px;
    overflow: hidden;
}

.markdown-body th,
.markdown-body td {
    border: 1px solid var(--border-color);
    padding: 12px 16px;
    text-align: left;
    white-space: pre-wrap;
    position: relative;
}

.markdown-body th::after,
.markdown-body td::after {
    content: "\200B";
    display: inline;
    pointer-events: none;
}

.markdown-body th {
    background: rgba(139, 92, 246, 0.1);
    color: var(--accent-color);
    font-weight: 600;
}

.markdown-body td[contenteditable="true"]:focus {
    background: rgba(139, 92, 246, 0.05);
    outline: 2px solid var(--accent-color);
    outline-offset: -2px;
}

.markdown-body blockquote {
    border-left: 4px solid var(--accent-color);
    padding-left: 1rem;
    color: var(--text-secondary);
    font-style: italic;
}

.markdown-body ul,
.markdown-body ol {
    padding-left: 1.5rem;
    margin: 0.5rem 0;
}

.markdown-body li {
    margin: 0.25rem 0;
}

.markdown-body li>p {
    margin: 0.25rem 0;
}

.markdown-body li>ul,
.markdown-body li>ol {
    margin: 0.1rem 0 0.1rem 1.2rem;
}

/* Diff View */
.diff-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--pane-bg);
    margin: 1rem;
    border-radius: 16px;
    border: 1px solid var(--border-color);
    overflow: hidden;
}

.diff-body {
    flex: 1;
    overflow-y: auto;
    background: var(--pane-header-bg);
}

.diff-table {
    width: 100%;
    border-collapse: collapse;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.85rem;
    line-height: 1.5;
}

.diff-tr {
    display: table-row;
}

.diff-td {
    display: table-cell;
    padding: 0 0.5rem;
    white-space: pre-wrap;
    word-break: break-all;
}

.diff-num {
    width: 3.5rem;
    text-align: right;
    color: var(--text-secondary);
    background: rgba(0, 0, 0, 0.05);
    user-select: none;
    border-right: 1px solid var(--border-color);
    vertical-align: top;
    padding-top: 2px;
}

.diff-indicator {
    width: 1.5rem;
    text-align: center;
    user-select: none;
    vertical-align: top;
    padding-top: 2px;
}

.diff-content {
    padding-left: 1rem !important;
}

/* Diff coloring */
.diff-line-added {
    background-color: rgba(63, 185, 80, 0.15);
}

.diff-line-added .diff-content {
    color: #aff5b4;
}

.diff-line-added .diff-indicator {
    color: #aff5b4;
}

.diff-line-removed {
    background-color: rgba(248, 81, 73, 0.15);
}

.diff-line-removed .diff-content {
    color: #ffa198;
}

.diff-line-removed .diff-indicator {
    color: #ffa198;
}

body.light-mode .diff-line-added {
    background-color: rgba(63, 185, 80, 0.2);
}

body.light-mode .diff-line-added .diff-content {
    color: #1a7f37;
}

body.light-mode .diff-line-removed {
    background-color: rgba(248, 81, 73, 0.2);
}

body.light-mode .diff-line-removed .diff-content {
    color: #cf222e;
}

.markdown-body input[type="checkbox"] {
    accent-color: var(--accent-color);
    width: 1.1rem;
    height: 1.1rem;
    vertical-align: middle;
    cursor: pointer;
}

/* Alerts (GitHub style) */
.markdown-body blockquote.alert {
    padding: 0.1rem 1rem;
    color: var(--text-primary);
    border-left-width: 4px;
    margin: 1.5rem 0;
    border-radius: 0 8px 8px 0;
    background: var(--alert-bg);
}

.markdown-body .alert-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 600;
    margin-top: 0.75rem;
    margin-bottom: 0.5rem;
    text-transform: uppercase;
    font-size: 0.85rem;
}

.alert-note {
    border-left-color: #0969da !important;
}

.alert-note .alert-title {
    color: #54aeff;
}

.alert-tip {
    border-left-color: #1a7f37 !important;
}

.alert-tip .alert-title {
    color: #3fb950;
}

.alert-important {
    border-left-color: #8250df !important;
}

.alert-important .alert-title {
    color: #a371f7;
}

.alert-warning {
    border-left-color: #9e6a03 !important;
}

.alert-warning .alert-title {
    color: #d29922;
}

.alert-caution {
    border-left-color: #cf222e !important;
}

.alert-caution .alert-title {
    color: #f85149;
}

/* Details/Summary */
.markdown-body details {
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.5rem 1rem;
    margin: 1rem 0;
    background: var(--details-bg);
}

.markdown-body summary {
    cursor: pointer;
    font-weight: 600;
    color: var(--accent-color);
    outline: none;
    margin: 0.25rem 0;
}

/* KaTeX */
.katex-display {
    margin: 1rem 0;
    overflow-x: auto;
    overflow-y: hidden;
}

/* Code Styles */
.markdown-body pre {
    background: var(--pre-bg);
    padding: 1.5rem;
    border-radius: 8px;
    overflow-x: auto;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.9em;
    border: 1px solid var(--border-color);
    margin: 1rem 0;
}

.markdown-body code {
    background: var(--code-bg);
    padding: 0.2em 0.4em;
    border-radius: 4px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.9em;
}

.markdown-body pre code {
    background: transparent;
    padding: 0;
}

/* Diagrams */
.mermaid {
    background: rgba(255, 255, 255, 0.02);
    border-radius: 8px;
    padding: 1.5rem;
    margin: 1rem 0;
    text-align: center;
    overflow-x: auto;
    border: 1px solid var(--border-color);
}

.plantuml-container {
    background: rgba(255, 255, 255, 0.9);
    border-radius: 8px;
    padding: 1.5rem;
    margin: 1rem 0;
    text-align: center;
    overflow-x: auto;
    border: 1px solid var(--border-color);
}

.plantuml-diagram {
    max-width: 100%;
    height: auto;
}

body.light-mode .mermaid,
body.light-mode .plantuml-container {
    background: rgba(0, 0, 0, 0.02);
}

/* GitHub Style Alerts */