/**
 * Fin-Studio PWA — Mobile-first styles
 * Based on Fin-Studio desktop design tokens.
 */

/* ===== Design Tokens ===== */
:root {
    --bg-primary: #141414;
    --bg-secondary: #1e1e1e;
    --bg-tertiary: #282828;
    --bg-hover: #363636;
    --bg-active: #0e5478;
    --text-primary: #ececec;
    --text-secondary: #b4b4b4;
    --text-muted: #8a8a8a;
    --border-color: rgba(255, 255, 255, 0.12);
    --accent-color: #4f94ff;
    --accent-hover: #2563eb;
    --accent-muted: rgba(79, 148, 255, 0.18);
    --danger-color: #f87171;
    --success-color: #34d399;
    --warning-color: #fbbf24;
    --font-size-xs: 11px;
    --font-size-sm: 13px;
    --font-size-md: 14px;
    --font-size-lg: 16px;
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 20px;
    --space-6: 24px;
    --space-8: 32px;
    --radius-xs: 4px;
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --transition-fast: 0.1s ease;
    --transition-normal: 0.2s ease;
    --header-height: 48px;
    --status-height: 26px;
    --editor-font-size: 17px;
    --editor-line-height: 1.75;
    --editor-max-width: 800px;
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Hiragino Kaku Gothic ProN', 'Hiragino Sans', 'Yu Gothic UI', 'Yu Gothic', 'Meiryo', 'Noto Sans JP', sans-serif;
    --font-serif: 'Hiragino Mincho ProN', 'Yu Mincho', 'Noto Serif JP', 'Times New Roman', serif;
    --font-mono: 'SF Mono', Menlo, Consolas, 'DejaVu Sans Mono', 'Noto Sans Mono CJK JP', 'Noto Sans JP', monospace;
}

[data-theme="light"] {
    --bg-primary: #ffffff;
    --bg-secondary: #f7f7f8;
    --bg-tertiary: #ececee;
    --bg-hover: #dedee1;
    --bg-active: #c9e0f5;
    --text-primary: #101014;
    --text-secondary: #44474d;
    --text-muted: #6e717a;
    --border-color: rgba(0, 0, 0, 0.14);
    --accent-color: #1d4ed8;
    --accent-hover: #1e3a8a;
    --accent-muted: rgba(29, 78, 216, 0.1);
    --danger-color: #b91c1c;
    --success-color: #15803d;
    --warning-color: #b45309;
}

/* ===== Reset ===== */
* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
    height: 100dvh;
}

body {
    font-family: var(--font-sans);
    font-size: var(--font-size-md);
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
    -webkit-text-size-adjust: 100%;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { touch-action: manipulation; }
:focus-visible { outline: 2px solid var(--accent-color); outline-offset: 2px; }

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(128,128,128,0.4); border-radius: 9999px; }
::-webkit-scrollbar-thumb:hover { background: rgba(128,128,128,0.6); }

/* ===== Header ===== */
#appHeader {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: calc(var(--header-height) + env(safe-area-inset-top, 0px));
    padding-top: env(safe-area-inset-top, 0px);
    display: flex;
    align-items: center;
    padding-left: var(--space-2);
    padding-right: var(--space-2);
    gap: var(--space-1);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    z-index: 100;
}

.nav-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--text-muted);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: color var(--transition-fast), background var(--transition-fast);
    flex-shrink: 0;
}

@media (hover: hover) {
    .nav-btn:hover { background: var(--bg-hover); color: var(--text-primary); }
}
.nav-btn:active { background: var(--bg-hover); color: var(--text-primary); transform: scale(0.95); }
.nav-btn.active { color: var(--accent-color); }
.nav-btn.active:active { background: var(--accent-muted); }

.header-title {
    font-size: var(--font-size-sm);
    font-weight: 500;
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    text-align: center;
    padding: 0 var(--space-2);
    min-width: 0;
    letter-spacing: 0.2px;
}

/* ===== Swipe Container ===== */
#swipeContainer {
    position: fixed;
    top: calc(var(--header-height) + env(safe-area-inset-top, 0px));
    left: 0;
    right: 0;
    bottom: calc(var(--status-height) + env(safe-area-inset-bottom, 0px));
    display: flex;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

#swipeContainer::-webkit-scrollbar { display: none; }

.panel {
    flex: 0 0 100%;
    scroll-snap-align: start;
    overflow: hidden;
}

.panel-inner {
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.panel-title {
    font-size: var(--font-size-sm);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--border-color);
}

/* ===== Status Bar ===== */
.status-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(var(--status-height) + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding-left: var(--space-3);
    padding-right: var(--space-3);
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    font-size: var(--font-size-xs);
    color: var(--text-muted);
    overflow-x: auto;
    scrollbar-width: none;
    z-index: 90;
}

.status-bar::-webkit-scrollbar { display: none; }

.stat {
    white-space: nowrap;
    flex-shrink: 0;
}

.stat-saved { margin-left: auto; }
.stat-saved.saving  { color: var(--warning-color); }
.stat-saved.ok      { color: var(--success-color); }
.stat-saved.dirty   { color: var(--warning-color); }
.stat-saved.error   { color: var(--danger-color); }

/* ===== Vault Switcher ===== */
.vault-switcher {
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-secondary);
}

.vault-trigger {
    display: flex;
    align-items: center;
    padding: var(--space-2) var(--space-4);
    cursor: pointer;
    min-height: 40px;
}

.vault-name {
    font-size: var(--font-size-sm);
    font-weight: 600;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.vault-icon {
    font-size: var(--font-size-xs);
    color: var(--text-muted);
    margin-right: var(--space-2);
    transition: transform var(--transition-fast);
}

.vault-dropdown {
    background: var(--bg-tertiary);
    max-height: 200px;
    overflow-y: auto;
}

.vault-item {
    padding: var(--space-3) var(--space-4);
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    cursor: pointer;
    border-bottom: 1px solid var(--border-color);
}

.vault-item:active, .vault-item.active {
    background: var(--bg-active);
    color: var(--text-primary);
}

/* ===== Recent Files ===== */
.recent-files {
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-primary);
}

.recent-header {
    font-size: var(--font-size-xs);
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: var(--space-2) var(--space-3) var(--space-1);
}

.recent-list {
    display: flex;
    flex-direction: column;
}

.recent-item {
    padding: var(--space-2) var(--space-3);
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    cursor: pointer;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    border-bottom: 1px solid var(--border-color);
}

.recent-item:hover { background: var(--bg-hover); }
.recent-item:active { background: var(--bg-hover); color: var(--text-primary); }
.recent-name { font-weight: 500; color: var(--text-primary); }

/* ===== Tree Toolbar ===== */
.tree-toolbar {
    display: flex;
    align-items: center;
    padding: var(--space-1) var(--space-2);
    gap: var(--space-1);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
}

.toolbar-btn {
    width: 40px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--text-muted);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: color var(--transition-fast), background var(--transition-fast);
}

@media (hover: hover) {
    .toolbar-btn:hover { background: var(--bg-hover); color: var(--text-primary); }
}
.toolbar-btn:active {
    background: var(--bg-hover);
    color: var(--text-primary);
    transform: scale(0.92);
}

/* ===== Search Bar ===== */
.search-bar {
    display: flex;
    align-items: center;
    padding: var(--space-2) var(--space-3);
    gap: var(--space-2);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
}

.search-bar input {
    flex: 1;
    padding: var(--space-2) var(--space-3);
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 16px;
    outline: none;
}

.search-bar input:focus {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px var(--accent-muted);
}

.search-close {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast), color var(--transition-fast);
}

@media (hover: hover) {
    .search-close:hover { background: var(--bg-hover); color: var(--text-primary); }
}
.search-close:active { background: var(--bg-hover); color: var(--text-primary); }

/* ===== Focus Indicator ===== */
.focus-indicator {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    background: var(--accent-muted);
    border-bottom: 1px solid var(--accent-color);
    color: var(--accent-color);
    font-size: var(--font-size-sm);
}

.focus-indicator svg { flex-shrink: 0; }

.focus-path {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.focus-clear {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--accent-color);
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    flex-shrink: 0;
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast);
}

@media (hover: hover) {
    .focus-clear:hover { background: var(--accent-muted); }
}
.focus-clear:active { background: var(--accent-muted); }

/* ===== File Tree ===== */
.file-tree {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-2) 0;
    -webkit-overflow-scrolling: touch;
}

.file-tree .placeholder {
    color: var(--text-muted);
    font-size: var(--font-size-sm);
    padding: var(--space-4);
    text-align: center;
}

.tree-item {
    display: flex;
    align-items: center;
    padding: var(--space-3) var(--space-3);
    cursor: pointer;
    user-select: none;
    font-size: var(--font-size-sm);
    transition: background 0.15s ease;
    min-height: 44px;
}

@media (hover: hover) {
    .tree-item:hover { background: var(--bg-hover); }
}
.tree-item:active { background: var(--bg-hover); }
.tree-item.active { background: var(--bg-active); box-shadow: inset 3px 0 0 var(--accent-color); color: var(--text-primary); }

.tree-item .icon {
    margin-right: var(--space-2);
    flex-shrink: 0;
    width: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
}

.tree-item .name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.tree-item.file-item {
    padding-left: calc(var(--space-3) + 20px);
}

.tree-item .toggle {
    width: 16px;
    text-align: center;
    color: var(--text-muted);
    flex-shrink: 0;
    margin-right: var(--space-1);
}

.tree-children { margin-left: var(--space-4); position: relative; }
.tree-children::before {
    content: '';
    position: absolute;
    left: 7px;
    top: 0;
    bottom: 0;
    width: 1px;
    background: var(--border-color);
    opacity: 0.5;
}
.tree-children.collapsed { display: none; }

.search-match-type {
    font-size: var(--font-size-xs);
    color: var(--text-muted);
    margin-left: var(--space-2);
    flex-shrink: 0;
}

.search-result {
    padding-left: var(--space-3) !important;
}

.search-dir {
    color: var(--text-muted);
    font-size: var(--font-size-xs);
}

/* ===== Editor Bar ===== */
.editor-bar {
    display: flex;
    align-items: center;
    padding: 0 var(--space-3);
    height: 44px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
    gap: var(--space-2);
}

.editor-file-name {
    flex: 1;
    font-size: var(--font-size-sm);
    font-weight: 500;
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.editor-actions {
    display: flex;
    align-items: center;
    gap: var(--space-1);
}

.icon-btn, .mode-toggle, .save-btn {
    width: 36px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    cursor: pointer;
    transition: color var(--transition-fast), background var(--transition-fast), border-color var(--transition-fast);
}

.icon-btn:hover, .mode-toggle:hover, .save-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.mode-toggle.preview-active {
    background: var(--accent-color);
    border-color: var(--accent-color);
    color: white;
}

.save-btn:not(:disabled) { color: var(--warning-color); border-color: var(--warning-color); }
.save-btn:disabled { opacity: 0.3; cursor: default; }

/* ===== Goal Bar ===== */
.goal-bar {
    position: relative;
    height: 18px;
    background: var(--bg-tertiary);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    flex-shrink: 0;
    overflow: hidden;
}

.goal-fill {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    background: var(--accent-muted);
    transition: width 0.3s ease;
}

.goal-fill.done { background: rgba(34, 197, 94, 0.25); }

.goal-text {
    position: relative;
    padding: 0 var(--space-3);
    font-size: var(--font-size-xs);
    color: var(--text-secondary);
    z-index: 1;
}

/* ===== Find Dialog ===== */
.find-dialog {
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    padding: var(--space-2) var(--space-3);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    flex-shrink: 0;
}

.find-row {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.find-row input {
    flex: 1;
    padding: var(--space-2) var(--space-3);
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 16px;
    outline: none;
    min-width: 0;
}

.find-row input:focus { border-color: var(--accent-color); }

.find-btn {
    padding: 0 var(--space-2);
    height: 36px;
    min-width: 36px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    cursor: pointer;
    font-size: var(--font-size-sm);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--transition-fast), color var(--transition-fast);
}

@media (hover: hover) {
    .find-btn:hover { background: var(--bg-hover); color: var(--text-primary); }
}
.find-btn:active { background: var(--bg-hover); color: var(--text-primary); }
.find-btn-text { padding: 0 var(--space-3); }

.find-count {
    font-size: var(--font-size-xs);
    color: var(--text-muted);
    min-width: 40px;
    text-align: center;
    flex-shrink: 0;
}

/* ===== Editor Textarea ===== */
.editor-wrap {
    flex: 1;
    position: relative;
    overflow: hidden;
}

.editor-textarea {
    width: 100%;
    height: 100%;
    max-width: var(--editor-max-width);
    margin: 0 auto;
    display: block;
    padding: var(--space-5) var(--space-5) 50vh;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-family: var(--font-mono);
    font-size: var(--editor-font-size);
    line-height: var(--editor-line-height);
    resize: none;
    outline: none;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    caret-color: var(--accent-color);
    letter-spacing: 0.01em;
}

.editor-textarea::placeholder { color: var(--text-muted); }
.editor-textarea::selection { background: var(--accent-muted); }

/* ===== Preview ===== */
.editor-preview {
    position: absolute;
    inset: 0;
    max-width: var(--editor-max-width);
    margin: 0 auto;
    padding: var(--space-6) var(--space-5) 50vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    font-size: var(--editor-font-size);
    line-height: var(--editor-line-height);
    color: var(--text-primary);
}

html.vertical .editor-preview {
    writing-mode: vertical-rl;
    text-orientation: upright;
    overflow-x: auto;
    overflow-y: hidden;
}

html.reading-mode .editor-preview {
    font-family: var(--font-serif);
    line-height: calc(var(--editor-line-height) + 0.1);
}
html.reading-mode .editor-preview h1,
html.reading-mode .editor-preview h2,
html.reading-mode .editor-preview h3,
html.reading-mode .editor-preview h4 {
    font-family: var(--font-sans);
}

/* Markdown */
.editor-preview h1, .editor-preview h2, .editor-preview h3,
.editor-preview h4, .editor-preview h5, .editor-preview h6 {
    margin-top: 1.6em;
    margin-bottom: 0.6em;
    font-weight: 700;
    line-height: 1.35;
    color: var(--text-primary);
    letter-spacing: -0.01em;
}

.editor-preview > :first-child { margin-top: 0; }
.editor-preview h1 { font-size: 1.75em; margin-top: 0.2em; padding-bottom: 0.25em; border-bottom: 1px solid var(--border-color); }
.editor-preview h2 { font-size: 1.4em; }
.editor-preview h3 { font-size: 1.2em; }
.editor-preview h4 { font-size: 1.05em; }

.editor-preview p { margin: 0.9em 0; }

.editor-preview ul, .editor-preview ol { margin: 0.9em 0; padding-left: 1.6em; }
.editor-preview li { margin: 0.4em 0; }
.editor-preview li > ul, .editor-preview li > ol { margin: 0.3em 0; }

.editor-preview a { color: var(--accent-color); text-decoration: underline; text-decoration-color: var(--accent-muted); text-underline-offset: 2px; }
.editor-preview a:hover { text-decoration-color: var(--accent-color); }
.editor-preview a:active { text-decoration: underline; }

.editor-preview code {
    background: var(--bg-tertiary);
    padding: 1px var(--space-2);
    border-radius: var(--radius-xs);
    font-family: var(--font-mono);
    font-size: 0.88em;
    letter-spacing: 0;
}

.editor-preview pre {
    background: var(--bg-tertiary);
    padding: var(--space-4);
    border-radius: var(--radius-sm);
    overflow-x: auto;
    margin: 1em 0;
}

.editor-preview pre code { background: none; padding: 0; font-size: 0.88em; line-height: 1.55; }

.editor-preview blockquote {
    border-left: 3px solid var(--accent-color);
    margin: 1em 0;
    padding: 0.1em var(--space-4);
    color: var(--text-secondary);
    background: var(--accent-muted);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.editor-preview table { border-collapse: collapse; width: 100%; margin: 1em 0; }
.editor-preview th, .editor-preview td {
    border: 1px solid var(--border-color);
    padding: var(--space-2) var(--space-3);
    text-align: left;
}
.editor-preview th { background: var(--bg-tertiary); }

.editor-preview hr { border: none; border-top: 1px solid var(--border-color); margin: 2em 0; }

.outline-title {
    min-height: 44px;
    padding: var(--space-3) var(--space-4);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    font-weight: 600;
}

.outline-view {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.outline-list {
    padding: var(--space-2) 0;
}

.outline-list .placeholder {
    color: var(--text-muted);
    font-size: var(--font-size-sm);
    padding: var(--space-4);
    text-align: center;
}

.outline-item {
    display: flex;
    align-items: center;
    padding: var(--space-3) var(--space-4);
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    cursor: pointer;
    min-height: 44px;
    transition: background var(--transition-fast);
}

@media (hover: hover) {
    .outline-item:hover { background: var(--bg-hover); color: var(--text-primary); }
}
.outline-item:active { background: var(--bg-hover); color: var(--text-primary); }

.outline-item.h1 { padding-left: var(--space-4); font-weight: 600; color: var(--text-primary); }
.outline-item.h2 { padding-left: var(--space-6); }
.outline-item.h3 { padding-left: calc(var(--space-6) + var(--space-3)); }
.outline-item.h4 { padding-left: calc(var(--space-8) + var(--space-4)); }
.outline-item.h5 { padding-left: calc(var(--space-8) + var(--space-6)); }
.outline-item.h6 { padding-left: calc(var(--space-8) + var(--space-8)); }

/* ===== Unsaved indicator ===== */
.editor-file-name.unsaved::after {
    content: ' *';
    color: var(--warning-color);
}

/* ===== Context Menu ===== */
.context-menu {
    position: fixed;
    z-index: 200;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: 0 8px 24px rgba(0,0,0,0.5);
    min-width: 180px;
    padding: var(--space-1) 0;
    overflow: hidden;
}

.ctx-item {
    padding: var(--space-3) var(--space-4);
    font-size: var(--font-size-sm);
    color: var(--text-primary);
    cursor: pointer;
    transition: background var(--transition-fast);
    min-height: 40px;
    display: flex;
    align-items: center;
}

@media (hover: hover) {
    .ctx-item:hover { background: var(--bg-hover); }
}
.ctx-item:active { background: var(--bg-active); }

.ctx-danger { color: var(--danger-color); }

.ctx-sep {
    height: 1px;
    background: var(--border-color);
    margin: var(--space-1) 0;
}

/* ===== Modal ===== */
.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 300;
    background: rgba(0,0,0,0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-4);
}

.modal-box {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: var(--space-6);
    width: 100%;
    max-width: 320px;
    box-shadow: 0 12px 40px rgba(0,0,0,0.6);
}

.modal-message {
    font-size: var(--font-size-md);
    color: var(--text-primary);
    margin-bottom: var(--space-4);
}

.modal-input {
    width: 100%;
    padding: var(--space-3);
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 16px;
    outline: none;
    margin-bottom: var(--space-4);
}

.modal-input:focus { border-color: var(--accent-color); box-shadow: 0 0 0 3px var(--accent-muted); }

.modal-buttons {
    display: flex;
    gap: var(--space-2);
    justify-content: flex-end;
}

.modal-btn {
    padding: var(--space-3) var(--space-5);
    min-height: 40px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: var(--font-size-sm);
    font-weight: 600;
    cursor: pointer;
    transition: background var(--transition-fast), opacity var(--transition-fast);
}

.modal-btn-cancel {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
}

.modal-btn-ok {
    background: var(--accent-color);
    color: white;
}

.modal-btn:active { opacity: 0.85; }

/* ===== Settings Modal ===== */
.settings-overlay {
    position: fixed;
    inset: 0;
    z-index: 350;
    background: rgba(0,0,0,0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-4);
}

.settings-box {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    width: 100%;
    max-width: 420px;
    max-height: 80dvh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 12px 40px rgba(0,0,0,0.6);
    overflow: hidden;
}

.settings-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-4);
    border-bottom: 1px solid var(--border-color);
}

.settings-title {
    font-size: var(--font-size-lg);
    font-weight: 600;
    color: var(--text-primary);
}

.settings-close {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast), color var(--transition-fast);
}

@media (hover: hover) {
    .settings-close:hover { background: var(--bg-hover); color: var(--text-primary); }
}
.settings-close:active { background: var(--bg-hover); color: var(--text-primary); }

.settings-body {
    padding: var(--space-4);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
}

.setting-row {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.setting-row label {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
}

.setting-row-toggle {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
}

.setting-row input[type="range"] {
    width: 100%;
    accent-color: var(--accent-color);
}

.setting-row input[type="checkbox"] {
    width: 44px;
    height: 24px;
    accent-color: var(--accent-color);
}

.setting-select {
    padding: var(--space-3) var(--space-3);
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 16px;
    outline: none;
    min-height: 40px;
}

.setting-select:focus { border-color: var(--accent-color); box-shadow: 0 0 0 3px var(--accent-muted); }

.setting-reset {
    padding: var(--space-3) var(--space-4);
    min-height: 40px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    cursor: pointer;
    transition: background var(--transition-fast), color var(--transition-fast);
}

@media (hover: hover) {
    .setting-reset:hover { background: var(--bg-hover); color: var(--text-primary); }
}
.setting-reset:active { background: var(--bg-hover); color: var(--text-primary); }

/* ===== Toast ===== */
.toast {
    position: fixed;
    bottom: calc(var(--status-height) + var(--space-4) + env(safe-area-inset-bottom, 0px));
    left: 50%;
    transform: translateX(-50%) translateY(80px);
    padding: var(--space-3) var(--space-6);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: var(--font-size-sm);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 16px rgba(0,0,0,0.4);
    z-index: 400;
    transition: transform 0.25s ease, opacity 0.25s ease;
    opacity: 0;
    pointer-events: none;
    white-space: nowrap;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

.toast.error {
    border-color: var(--danger-color);
    color: var(--danger-color);
}

/* ===== Focus Mode =====
 * Dim background chrome but keep it readable/tappable without hover.
 * Buttons reveal on hover (pointer) AND focus-within (keyboard/touch focus).
 * Status bar has no focusable children, so keep it readable at 0.75 baseline.
 * Save button :disabled stays dim — don't override it in focus mode.
 */
body.focus-mode #appHeader .nav-btn:not(#btnFocus),
body.focus-mode .editor-bar .icon-btn,
body.focus-mode .editor-bar .save-btn:not(:disabled) {
    opacity: 0.5;
    transition: opacity 0.25s ease;
}
body.focus-mode #appHeader .header-title { opacity: 0.65; transition: opacity 0.25s ease; }
body.focus-mode .status-bar { opacity: 0.75; transition: opacity 0.25s ease; }

body.focus-mode #appHeader:hover .nav-btn,
body.focus-mode #appHeader:focus-within .nav-btn,
body.focus-mode #appHeader:hover .header-title,
body.focus-mode #appHeader:focus-within .header-title,
body.focus-mode .status-bar:hover,
body.focus-mode .editor-bar:hover .icon-btn,
body.focus-mode .editor-bar:hover .save-btn:not(:disabled),
body.focus-mode .editor-bar:focus-within .icon-btn,
body.focus-mode .editor-bar:focus-within .save-btn:not(:disabled) {
    opacity: 1;
}

/* ===== Reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ===== Mobile (narrow viewport) polish ===== */
@media (max-width: 480px) {
    :root {
        --editor-font-size: 17px;
        --editor-line-height: 1.75;
    }
    .editor-textarea, .editor-preview {
        padding-left: var(--space-4);
        padding-right: var(--space-4);
    }
    .status-bar {
        gap: var(--space-2);
        padding-left: var(--space-3);
        padding-right: var(--space-3);
    }
    .settings-box { max-height: 90dvh; }
}
