* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    /* Stop iOS from re-flowing text when the device rotates. */
    -webkit-text-size-adjust: 100%;
}

body {
    --bg-primary: #1e1e1e;
    --bg-secondary: #252526;
    --bg-tertiary: #2d2d2d;
    --border-color: #333;
    --text-primary: #d4d4d4;
    --text-secondary: #6a6a6a;
    --accent-blue: #569cd6;
    --accent-teal: #4ec9b0;
    --accent-purple: #c586c0;
    --accent-yellow: #dcdcaa;
    --accent-orange: #ce9178;
    --accent-link: #4fc1ff;
    --accent-quote: #9cdcfe;
    --accent-action: #0e639c;
    --accent-action-hover: #1177bb;
    --danger: #dc3c3c;
    --shadow: rgba(0, 0, 0, 0.45);
    --scrollbar-track: #1e1e1e;
    --scrollbar-thumb: #424242;
    --scrollbar-thumb-hover: #4e4e4e;

    --toolbar-pad: 10px;
    --safe-l: env(safe-area-inset-left, 0px);
    --safe-r: env(safe-area-inset-right, 0px);
    --safe-t: env(safe-area-inset-top, 0px);
    --safe-b: env(safe-area-inset-bottom, 0px);

    background-color: var(--bg-primary);
    color: var(--text-primary);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    height: 100vh;      /* fallback for browsers with no dvh unit */
    overflow: hidden;
    display: flex;
    flex-direction: column;
    overscroll-behavior: none;
}

/* dvh tracks the viewport as the mobile browser chrome slides away. --vvh is set
   from visualViewport, because dvh does not shrink for the on-screen keyboard. */
@supports (height: 100dvh) {
    body {
        height: var(--vvh, 100dvh);
    }
}

body.light-mode {
    --bg-primary: #ffffff;
    --bg-secondary: #f5f5f5;
    --bg-tertiary: #e8e8e8;
    --border-color: #d0d0d0;
    --text-primary: #1e1e1e;
    --text-secondary: #6a6a6a;
    --accent-blue: #0066cc;
    --accent-teal: #008080;
    --accent-purple: #a31ea3;
    --accent-yellow: #795e26;
    --accent-orange: #a31515;
    --accent-link: #0066cc;
    --accent-quote: #005cc5;
    --shadow: rgba(0, 0, 0, 0.18);
    --scrollbar-track: #f5f5f5;
    --scrollbar-thumb: #c0c0c0;
    --scrollbar-thumb-hover: #a0a0a0;
}

/* ==========================================================================
   Toolbar
   ========================================================================== */

.toolbar {
    background-color: var(--bg-secondary);
    padding: var(--toolbar-pad);
    padding-top: calc(var(--toolbar-pad) + var(--safe-t));
    padding-left: calc(var(--toolbar-pad) + var(--safe-l));
    padding-right: calc(var(--toolbar-pad) + var(--safe-r));
    display: flex;
    gap: 10px;
    border-bottom: 1px solid var(--border-color);
    align-items: center;
    position: relative;
    flex-shrink: 0;
}

.toolbar-spacer {
    flex: 1;
}

.toolbar button,
.overflow button {
    background-color: var(--border-color);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    padding: 8px 16px;
    font-size: 13px;
    font-family: inherit;
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s;
    border-radius: 3px;
    white-space: nowrap;
    flex-shrink: 0;
    /* Kill the grey tap flash and the 300ms delay on touch. */
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.toolbar button.active,
.overflow button.active {
    background-color: var(--accent-action);
    color: #ffffff;
    border-color: var(--accent-action);
}

.toolbar button:active,
.overflow button:active {
    transform: scale(0.97);
    filter: brightness(1.25);
}

.toolbar button:focus-visible,
.overflow button:focus-visible,
.tab:focus-visible,
.new-tab-btn:focus-visible {
    outline: 2px solid var(--accent-blue);
    outline-offset: 2px;
}

.btn-primary {
    background-color: var(--accent-action) !important;
    border-color: var(--accent-action) !important;
    color: #ffffff !important;
}

/* Segmented view switch */
.view-switch {
    display: flex;
    flex-shrink: 0;
}

.view-switch button {
    border-radius: 0;
}

.view-switch button:first-child {
    border-radius: 3px 0 0 3px;
}

.view-switch button:last-child {
    border-radius: 0 3px 3px 0;
    margin-left: -1px;
}

/* Two labels per button: the wide one on desktop, the short one on phones. */
.lbl-narrow {
    display: none;
}

.icon-btn {
    width: 32px;
    height: 32px;
    padding: 0 !important;
    border-radius: 50%;
    font-weight: bold;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

/* The rare actions live in a panel anchored under the bar, at every width. */
.overflow {
    position: absolute;
    top: calc(100% + 6px);
    right: calc(10px + var(--safe-r));
    left: auto;
    display: none;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 8px;
    box-shadow: 0 6px 20px var(--shadow);
    z-index: 100;
    min-width: 200px;
    max-height: calc(100vh - 120px);
    overflow-y: auto;
}

.overflow.open {
    display: flex;
}

.overflow button {
    text-align: left;
    width: 100%;
    background-color: transparent;
    border-color: transparent;
}

/* The narrow label is an icon, so the button centres it like the icon buttons. */
#toc-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

#toc-btn .lbl-narrow svg {
    display: block;
}

/* Inside the menu the statistics read as a header for the list below. */
.overflow .doc-stats {
    padding: 4px 10px 10px;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 4px;
}

/* A thin rule groups the menu without adding a heading for each group. */
.menu-sep {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 4px 2px;
}

/* Reset throws the text away, so it reads as the one risky entry. */
.overflow button.menu-danger {
    color: var(--danger);
}

.backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 90;
}

.backdrop.hidden {
    display: none;
}

/* ==========================================================================
   Table of contents drawer
   ========================================================================== */

.toc-drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 300px;
    max-width: 85vw;
    z-index: 110;
    display: flex;
    flex-direction: column;
    background: var(--bg-secondary);
    border-left: 1px solid var(--border-color);
    box-shadow: -4px 0 16px var(--shadow);
    transform: translateX(100%);
    transition: transform 0.18s ease;
    visibility: hidden;
    padding-top: var(--safe-t);
    padding-right: var(--safe-r);
    padding-bottom: var(--safe-b);
}

.toc-drawer.open {
    transform: translateX(0);
    visibility: visible;
}

.toc-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 12px 8px 12px 16px;
    border-bottom: 1px solid var(--border-color);
    font-size: 13px;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-secondary);
    flex-shrink: 0;
}

/* The button colours live under .toolbar, so the drawer sets its own. */
.toc-head .icon-btn {
    background: none;
    border: 1px solid transparent;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 20px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.toc-head .icon-btn:active {
    background: var(--bg-tertiary);
}

.toc-head .icon-btn:focus-visible {
    outline: 2px solid var(--accent-blue);
    outline-offset: 2px;
}

.toc-list {
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: 8px 0 16px;
    flex: 1;
}

.toc-link {
    display: block;
    width: 100%;
    text-align: left;
    background: none;
    border: none;
    border-left: 2px solid transparent;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 14px;
    line-height: 1.35;
    padding: 8px 16px;
    cursor: pointer;
    overflow-wrap: break-word;
}

.toc-link.lvl-1 { font-weight: 600; }
.toc-link.lvl-2 { padding-left: 28px; }
.toc-link.lvl-3 { padding-left: 40px; font-size: 13px; }
.toc-link.lvl-4 { padding-left: 52px; font-size: 13px; color: var(--text-secondary); }
.toc-link.lvl-5 { padding-left: 64px; font-size: 13px; color: var(--text-secondary); }
.toc-link.lvl-6 { padding-left: 76px; font-size: 13px; color: var(--text-secondary); }

.toc-link.current {
    border-left-color: var(--accent-blue);
    color: var(--accent-blue);
    background: var(--bg-tertiary);
}

.toc-link:active {
    background: var(--bg-tertiary);
}

.toc-link:focus-visible {
    outline: 2px solid var(--accent-blue);
    outline-offset: -2px;
}

.toc-empty {
    padding: 16px;
    font-size: 13px;
    color: var(--text-secondary);
}

/* ==========================================================================
   Help popup
   ========================================================================== */

.help-popup {
    position: absolute;
    right: calc(10px + var(--safe-r));
    top: calc(100% + 6px);
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 10px 14px;
    font-size: 13px;
    line-height: 1.9;
    z-index: 100;
    box-shadow: 0 4px 12px var(--shadow);
    max-width: calc(100vw - 20px);
}

.help-popup.hidden {
    display: none;
}

.help-popup kbd {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 3px;
    padding: 2px 6px;
    font-family: inherit;
    font-size: 11px;
    margin-right: 8px;
}

.help-popup hr {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 8px 0;
}

.touch-only {
    display: none;
}

/* Save indicator */
.save-indicator {
    font-size: 12px;
    color: var(--text-secondary);
    opacity: 0;
    transition: opacity 0.3s ease;
    white-space: nowrap;
    pointer-events: none;
}

.save-indicator.visible {
    opacity: 1;
}

/* ==========================================================================
   Tab bar
   ========================================================================== */

.tab-bar {
    display: flex;
    background-color: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    padding: 0 10px;
    padding-left: calc(10px + var(--safe-l));
    padding-right: calc(10px + var(--safe-r));
    gap: 2px;
    align-items: flex-end;
    min-height: 36px;
    overflow-x: auto;
    overflow-y: hidden;
    flex-shrink: 0;
    scrollbar-width: none;              /* Firefox */
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
}

.tab-bar::-webkit-scrollbar {
    display: none;                      /* Chrome, Safari */
}

.tab {
    display: flex;
    align-items: center;
    gap: 8px;
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-bottom: none;
    padding: 6px 12px;
    font-size: 13px;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 4px 4px 0 0;
    max-width: 180px;
    flex-shrink: 0;
    transition: background-color 0.15s, color 0.15s;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.tab.active {
    background-color: var(--bg-primary);
    color: var(--text-primary);
    border-bottom: 1px solid var(--bg-primary);
    margin-bottom: -1px;
}

.tab-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    min-width: 0;
    /* Stop the browser from selecting the label during a long press. */
    -webkit-user-select: none;
    user-select: none;
}

.tab-name-input {
    background: var(--bg-primary);
    border: 1px solid var(--accent-blue);
    color: var(--text-primary);
    font-size: 16px;                    /* 16px stops iOS from zooming on focus */
    font-family: inherit;
    padding: 0 4px;
    width: 100%;
    min-width: 80px;
    outline: none;
    border-radius: 2px;
}

.tab-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 16px;
    cursor: pointer;
    padding: 0;
    width: 20px;
    height: 20px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    border-radius: 3px;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.tab.confirm-delete {
    background-color: rgba(220, 60, 60, 0.3);
    border-color: var(--danger);
}

.tab.confirm-delete .tab-close {
    background-color: var(--danger);
    color: white;
}

.tab.shake {
    animation: shake 0.3s ease-in-out;
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-4px); }
    75% { transform: translateX(4px); }
}

.new-tab-btn {
    background-color: transparent;
    border: 1px dashed var(--border-color);
    color: var(--text-secondary);
    padding: 6px 12px;
    font-size: 13px;
    font-family: inherit;
    cursor: pointer;
    border-radius: 4px 4px 0 0;
    transition: background-color 0.15s, color 0.15s;
    margin-left: 4px;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

/* ==========================================================================
   Panes
   ========================================================================== */

.container {
    flex: 1;
    background-color: var(--bg-primary);
    overflow: hidden;
    min-height: 0;
}

.container.split-mode {
    display: flex;
}

.resizer {
    width: 1px;
    background-color: var(--border-color);
    cursor: col-resize;
    flex-shrink: 0;
    position: relative;
    transition: background-color 0.15s;
    touch-action: none;                 /* the pointer handler owns this gesture */
}

/* Invisible 16px grab strip, so the 1px line is still easy to hit. */
.resizer::before {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    left: -8px;
    right: -8px;
}

.resizer.dragging {
    background-color: var(--accent-blue);
    box-shadow: 0 0 0 1px var(--accent-blue);
}

/* The split ratio lives in a custom property so a media query can override it. */
.container.split-mode .input-section {
    flex: 0 0 var(--split, 50%);
    min-width: 80px;
}

.container.split-mode .output-section {
    flex: 1 1 0;
    min-width: 80px;
}

.container.full-mode .input-section {
    display: none;
}

.container.full-mode .output-section {
    display: block;
}

.input-section {
    background-color: var(--bg-primary);
    overflow: hidden;
    height: 100%;
}

.output-section {
    background-color: var(--bg-primary);
    overflow-y: auto;
    overflow-x: hidden;
    height: 100%;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding-bottom: var(--safe-b);
}

#markdown-input {
    width: 100%;
    height: 100%;
    background-color: var(--bg-secondary);
    color: var(--text-primary);
    border: none;
    padding: 20px;
    padding-left: calc(20px + var(--safe-l));
    padding-bottom: calc(20px + var(--safe-b));
    font-family: 'Consolas', 'Courier New', monospace;
    font-size: 14px;
    line-height: 1.6;
    resize: none;
    outline: none;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

#markdown-input::placeholder {
    color: var(--text-secondary);
}

/* ==========================================================================
   Rendered markdown
   ========================================================================== */

#markdown-output {
    padding: 20px;
    padding-left: calc(20px + var(--safe-l));
    padding-right: calc(20px + var(--safe-r));
    line-height: 1.6;
    /* Long URLs break instead of pushing the page wide. Use break-word, not
       anywhere: anywhere also shrinks intrinsic width and would crush tables. */
    overflow-wrap: break-word;
}

#markdown-output h1 {
    color: var(--accent-blue);
    margin: 20px 0 10px 0;
    font-size: 2em;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 5px;
}

#markdown-output h2 {
    color: var(--accent-teal);
    margin: 18px 0 8px 0;
    font-size: 1.5em;
}

#markdown-output h3 {
    color: var(--accent-purple);
    margin: 16px 0 8px 0;
    font-size: 1.25em;
}

#markdown-output h4,
#markdown-output h5,
#markdown-output h6 {
    color: var(--accent-yellow);
    margin: 14px 0 6px 0;
}

#markdown-output p {
    margin: 10px 0;
}

#markdown-output code {
    background-color: var(--bg-tertiary);
    color: var(--accent-orange);
    padding: 2px 6px;
    border-radius: 3px;
    font-family: 'Consolas', 'Courier New', monospace;
    font-size: 0.9em;
}

#markdown-output pre {
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 5px;
    padding: 15px;
    overflow-x: auto;
    margin: 10px 0;
    -webkit-overflow-scrolling: touch;
}

#markdown-output pre code {
    background-color: transparent;
    padding: 0;
    color: var(--text-primary);
    /* Code keeps its own line breaks; the block scrolls instead. */
    overflow-wrap: normal;
    word-break: normal;
    white-space: pre;
}

#markdown-output blockquote {
    border-left: 4px solid var(--accent-blue);
    padding-left: 15px;
    margin: 10px 0;
    color: var(--accent-quote);
    font-style: italic;
}

#markdown-output ul,
#markdown-output ol {
    margin: 10px 0;
    padding-left: 30px;
}

#markdown-output li {
    margin: 5px 0;
}

#markdown-output a {
    color: var(--accent-link);
    text-decoration: none;
}

/* A wide table scrolls inside its own box instead of clipping. */
.table-wrap {
    overflow-x: auto;
    margin: 10px 0;
    -webkit-overflow-scrolling: touch;
}

/* Fill the pane when the table is narrow, but grow and scroll when it is wide,
   instead of crushing every column to fit. */
#markdown-output table {
    border-collapse: collapse;
    width: auto;
    min-width: 100%;
}

#markdown-output table th,
#markdown-output table td {
    border: 1px solid var(--border-color);
    padding: 8px 12px;
    overflow-wrap: normal;
}

#markdown-output table th {
    background-color: var(--bg-tertiary);
    color: var(--accent-blue);
}

#markdown-output hr {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 20px 0;
}

#markdown-output img {
    max-width: 100%;
    height: auto;
}

/* Task lists stay clickable in the preview, and get a bigger box for touch. */
#markdown-output li:has(> input[type="checkbox"]) {
    list-style: none;
    margin-left: -22px;
}

#markdown-output input[type="checkbox"] {
    width: 16px;
    height: 16px;
    margin-right: 8px;
    vertical-align: -2px;
    accent-color: var(--accent-action);
    cursor: pointer;
}

/* ==========================================================================
   Syntax colour. The tokens reuse the app palette, so both themes work with
   no second stylesheet.
   ========================================================================== */

.hljs-comment,
.hljs-quote {
    color: var(--text-secondary);
    font-style: italic;
}

.hljs-keyword,
.hljs-selector-tag,
.hljs-literal,
.hljs-doctag,
.hljs-formula {
    color: var(--accent-purple);
}

.hljs-string,
.hljs-regexp,
.hljs-addition,
.hljs-attribute,
.hljs-meta .hljs-string {
    color: var(--accent-orange);
}

.hljs-number,
.hljs-variable,
.hljs-template-variable,
.hljs-type,
.hljs-selector-attr,
.hljs-selector-pseudo,
.hljs-link {
    color: var(--accent-teal);
}

.hljs-title,
.hljs-section,
.hljs-selector-id,
.hljs-title.class_,
.hljs-title.function_ {
    color: var(--accent-yellow);
}

.hljs-attr,
.hljs-name,
.hljs-built_in,
.hljs-builtin-name,
.hljs-symbol,
.hljs-bullet,
.hljs-meta {
    color: var(--accent-blue);
}

.hljs-deletion {
    color: #e06c75;
}

.hljs-emphasis { font-style: italic; }
.hljs-strong { font-weight: bold; }

/* ==========================================================================
   Document statistics
   ========================================================================== */

.doc-stats {
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
    align-self: center;
    padding: 0 2px;
}

.doc-stats:empty {
    display: none;
}

/* ==========================================================================
   File open
   ========================================================================== */

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

.drop-hint {
    position: fixed;
    inset: 0;
    z-index: 300;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(14, 99, 156, 0.18);
    border: 3px dashed var(--accent-action);
    color: var(--text-primary);
    font-size: 18px;
    font-weight: 600;
    pointer-events: none;
}

.drop-hint.hidden {
    display: none;
}

/* ==========================================================================
   Toast
   ========================================================================== */

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(20px + var(--safe-b));
    transform: translateX(-50%);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px 14px;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 14px;
    box-shadow: 0 6px 20px var(--shadow);
    z-index: 200;
    max-width: calc(100vw - 32px);
    animation: toast-in 0.2s ease;
}

.toast.hidden {
    display: none;
}

@keyframes toast-in {
    from { opacity: 0; transform: translate(-50%, 12px); }
    to   { opacity: 1; transform: translate(-50%, 0); }
}

.toast-action {
    background: none;
    border: none;
    color: var(--accent-link);
    font-family: inherit;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    padding: 6px 4px;
    min-height: 32px;
    -webkit-tap-highlight-color: transparent;
}

.toast-action.hidden {
    display: none;
}

/* ==========================================================================
   Hover states — only where a real pointer exists.
   On touch, :hover sticks after a tap and looks like a stuck selection.
   ========================================================================== */

@media (hover: hover) and (pointer: fine) {
    .toolbar button:hover,
    .toc-head .icon-btn:hover {
        background: var(--bg-tertiary);
        color: var(--text-primary);
    }

    .toc-link:hover {
        background: var(--bg-tertiary);
    }

    .overflow button:hover {
        background-color: var(--border-color);
        color: var(--text-primary);
    }

    .overflow button.menu-danger:hover {
        color: var(--danger);
    }

    .toolbar button.active:hover,
    .overflow button.active:hover {
        background-color: var(--accent-action-hover);
    }

    .btn-primary:hover {
        background-color: var(--accent-action-hover) !important;
    }

    .tab:hover {
        background-color: var(--bg-primary);
        color: var(--text-primary);
    }

    .tab-close:hover {
        background-color: var(--danger);
        color: #fff;
    }

    .new-tab-btn:hover {
        background-color: var(--bg-tertiary);
        color: var(--text-primary);
        border-style: solid;
    }

    .resizer:hover {
        background-color: var(--accent-blue);
    }

    #markdown-output a:hover {
        text-decoration: underline;
    }

    .toast-action:hover {
        text-decoration: underline;
    }
}

/* ==========================================================================
   Touch devices — bigger targets
   ========================================================================== */

@media (hover: none), (max-width: 520px) {
    /* Under 16px, iOS Safari zooms the page when the field takes focus. */
    #markdown-input {
        font-size: 16px;
    }

    .toolbar button,
    .overflow button {
        min-height: 44px;
        font-size: 14px;
    }

    .icon-btn {
        width: 44px;
        height: 44px;
    }

    .tab {
        min-height: 44px;
        padding: 6px 10px 6px 14px;
        font-size: 14px;
    }

    .tab-close {
        width: 34px;
        height: 34px;
        font-size: 20px;
    }

    .new-tab-btn {
        min-height: 44px;
        font-size: 14px;
    }

    /* The narrow Contents label is an icon, so give it a full touch target. */
    #toc-btn {
        min-width: 44px;
        padding-left: 8px;
        padding-right: 8px;
    }

    .toc-link {
        min-height: 44px;
        padding-top: 11px;
        padding-bottom: 11px;
    }

    .touch-only {
        display: block;
    }
}

/* ==========================================================================
   Phones — one pane at a time, secondary actions inside a menu
   ========================================================================== */

@media (max-width: 520px) {
    body {
        --toolbar-pad: 8px;
    }

    .toolbar {
        gap: 8px;
    }

    .lbl-wide {
        display: none;
    }

    .lbl-narrow {
        display: inline;
    }

    /* Split view cannot work at this width, so each mode shows one pane. */
    .container.split-mode {
        display: block;
    }

    .container.split-mode .input-section {
        display: block;
        width: 100%;
        height: 100%;
    }

    .container.split-mode .output-section {
        display: none;
    }

    .container.full-mode .output-section {
        width: 100%;
        height: 100%;
    }

    .resizer {
        display: none;
    }

    /* Sync scroll needs two visible panes, so it has no meaning here. */
    #sync-scroll-btn {
        display: none;
    }

    .tab-bar {
        min-height: 44px;
        padding-bottom: 0;
    }

    .tab {
        max-width: 150px;
    }

    #markdown-output {
        padding: 16px;
        padding-left: calc(16px + var(--safe-l));
        padding-right: calc(16px + var(--safe-r));
        padding-bottom: calc(24px + var(--safe-b));
    }

    #markdown-output h1 {
        font-size: 1.6em;
        margin-top: 14px;
    }

    #markdown-output h2 {
        font-size: 1.35em;
    }

    #markdown-output h3 {
        font-size: 1.15em;
    }

    #markdown-output ul,
    #markdown-output ol {
        padding-left: 22px;
    }

    #markdown-input {
        padding: 16px;
        padding-left: calc(16px + var(--safe-l));
        padding-bottom: calc(16px + var(--safe-b));
    }

    .help-popup {
        right: calc(8px + var(--safe-r));
        font-size: 14px;
    }
}

/* Very narrow phones. Every control must still fit on one row, so tighten the
   spacing and drop the save indicator: the app saves on its own anyway. */
@media (max-width: 400px) {
    .toolbar {
        gap: 4px;
    }

    .toolbar button {
        padding: 8px 7px;
    }

    .icon-btn,
    #toc-btn {
        width: 40px;
        min-width: 40px;
        height: 40px;
    }
}

/* The smallest phones have no room left, so drop the save indicator. The app
   saves on its own, and a failed save still raises a toast. */
@media (max-width: 374px) {
    .save-indicator {
        display: none;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ==========================================================================
   Print — the preview alone, on white, with no app chrome
   ========================================================================== */

@media print {
    /* The app locks the height for the screen layout. Printing needs the page
       to grow, or only the first sheet comes out. */
    html,
    body {
        height: auto !important;
        overflow: visible !important;
        display: block !important;
        background: #fff !important;
        color: #000 !important;
    }

    .toolbar,
    .tab-bar,
    .resizer,
    .input-section,
    .toast,
    .backdrop,
    .drop-hint,
    .overflow,
    .toc-drawer,
    .help-popup {
        display: none !important;
    }

    .container,
    .container.split-mode,
    .container.full-mode {
        display: block !important;
        overflow: visible !important;
        height: auto !important;
    }

    .output-section {
        display: block !important;
        overflow: visible !important;
        height: auto !important;
        width: 100% !important;
        padding: 0 !important;
    }

    #markdown-output {
        padding: 0 !important;
        color: #000;
        font-size: 11pt;
        line-height: 1.5;
    }

    /* Ink saving: print every heading in black, not in the screen accents. */
    #markdown-output h1,
    #markdown-output h2,
    #markdown-output h3,
    #markdown-output h4,
    #markdown-output h5,
    #markdown-output h6,
    #markdown-output th {
        color: #000 !important;
    }

    #markdown-output pre,
    #markdown-output code,
    #markdown-output th {
        background: #f4f4f4 !important;
        color: #000 !important;
    }

    #markdown-output pre {
        white-space: pre-wrap;      /* no clipped code on paper */
        overflow: visible !important;
        border: 1px solid #ccc;
    }

    #markdown-output [class^="hljs-"],
    #markdown-output [class*=" hljs-"] {
        color: #000 !important;
    }

    #markdown-output a {
        color: #000 !important;
        text-decoration: underline;
    }

    /* Show where a link points, since paper cannot be clicked. */
    #markdown-output a[href^="http"]::after {
        content: " (" attr(href) ")";
        font-size: 0.85em;
        word-break: break-all;
    }

    #markdown-output blockquote {
        color: #333 !important;
        border-left: 3px solid #999;
    }

    .table-wrap {
        overflow: visible !important;
    }

    #markdown-output table {
        width: 100% !important;
    }

    /* Keep blocks whole across a page break where the browser allows it. */
    #markdown-output pre,
    #markdown-output table,
    #markdown-output blockquote,
    #markdown-output img {
        break-inside: avoid;
    }

    #markdown-output h1,
    #markdown-output h2,
    #markdown-output h3 {
        break-after: avoid;
    }
}

/* ==========================================================================
   Scrollbars (desktop only — mobile browsers draw their own overlay)
   ========================================================================== */

@media (hover: hover) and (pointer: fine) {
    ::-webkit-scrollbar {
        width: 12px;
        height: 12px;
    }

    ::-webkit-scrollbar-track {
        background: var(--scrollbar-track);
    }

    ::-webkit-scrollbar-thumb {
        background: var(--scrollbar-thumb);
        border-radius: 6px;
    }

    ::-webkit-scrollbar-thumb:hover {
        background: var(--scrollbar-thumb-hover);
    }
}

/* The portfolio frame can be narrower than a full mobile browser. */
html { overflow: hidden; }
@media (max-width: 360px) {
    .toolbar { flex-wrap: wrap; }
    .toolbar-spacer { display: none; }
    .toolbar button { padding-left: 8px; padding-right: 8px; }
}

@media (max-width: 768px) { .toolbar { flex-wrap: wrap; } }
