/* ==========================================================================
   App shell: sky stage, top bar (logo, formatting toolbar, save state and
   actions), document card, panes, splitter and status bar
   ========================================================================== */

.app {
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    background: var(--bg);
}

/* --- Sky stage ----------------------------------------------------------- */

.stage {
    position: relative;
    flex: 1;
    min-height: 0;
    margin: 10px;
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    overflow: hidden;
    border-radius: var(--radius-stage);
    background: var(--sky);
}

.sky {
    position: absolute;
    inset: 0;
    pointer-events: none;
}

.cloud {
    position: absolute;
    border-radius: 50%;
    background: var(--cloud);
    opacity: var(--cloud-opacity);
}

.cloud-1 {
    left: 4%;
    top: 30px;
    width: 380px;
    height: 200px;
    filter: blur(52px);
}

.cloud-2 {
    left: 40%;
    top: -110px;
    width: 560px;
    height: 260px;
    filter: blur(64px);
    opacity: calc(var(--cloud-opacity) * 0.77);
}

.cloud-3 {
    right: 4%;
    top: 40px;
    width: 400px;
    height: 200px;
    filter: blur(52px);
}

.sky-dots {
    position: absolute;
    inset: 0;
    background-image: radial-gradient(var(--sky-dots) 1px, transparent 1.5px);
    background-size: 16px 16px;
    -webkit-mask-image: linear-gradient(180deg, #000 0%, rgba(0, 0, 0, 0.3) 22%, transparent 45%);
    mask-image: linear-gradient(180deg, #000 0%, rgba(0, 0, 0, 0.3) 22%, transparent 45%);
}

/* --- Top bar ------------------------------------------------------------- */

.app-header {
    position: relative;
    z-index: 5;
    flex: none;
    min-height: 52px;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 0 8px 0 6px;
    background: var(--toolbar-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-toolbar);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
}

/* Logo: opens the file menu */
.brand-btn {
    height: 40px;
    flex: none;
    padding: 0 6px 0 4px;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    border: 0;
    border-radius: var(--radius);
    background: transparent;
    color: var(--muted);
    transition: background-color 0.15s ease;
}

.brand-btn:hover {
    background: var(--hover-strong);
}

.brand-btn[aria-expanded="true"] {
    background: var(--pressed);
}

.brand-mark {
    width: 32px;
    height: 32px;
    display: grid;
    place-items: center;
    border-radius: var(--radius-control);
    background: var(--ink);
    color: var(--on-ink);
    font-size: 13.5px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.chevron {
    transition: transform 0.15s ease;
}

[aria-expanded="true"] > .chevron {
    transform: rotate(180deg);
}

.header-sep {
    width: 1px;
    height: 22px;
    flex: none;
    margin: 0 6px;
    background: var(--border);
}

/* --- Formatting toolbar -------------------------------------------------- */

.toolbar {
    flex: 0 1 auto;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 2px;
    /* Room for focus rings, which the scroll container would otherwise clip */
    padding: 4px;
    margin: -4px;
    overflow-x: auto;
    scrollbar-width: none;
}

.toolbar::-webkit-scrollbar {
    display: none;
}

.toolbar > * {
    flex: none;
}

.tb {
    width: 36px;
    height: 36px;
    padding: 0;
    display: inline-grid;
    place-items: center;
    border: 0;
    border-radius: var(--radius-control);
    background: transparent;
    color: var(--text-control);
    transition: background-color 0.15s ease, opacity 0.15s ease;
}

.tb:hover:not([aria-disabled="true"]) {
    background: var(--hover-strong);
}

.tb[aria-pressed="true"],
.tb[aria-expanded="true"] {
    background: var(--pressed);
    color: var(--text);
}

.tb[aria-disabled="true"] {
    opacity: 0.38;
    cursor: default;
}

.tb-wide {
    width: auto;
    min-width: 76px;
    padding: 0 6px 0 10px;
    display: inline-flex;
    align-items: center;
    justify-content: space-between;
    gap: 4px;
    font-size: 14px;
    font-weight: 500;
}

.tb-wide svg {
    color: var(--muted);
}

.tb-sep {
    width: 1px;
    height: 20px;
    margin: 0 5px;
    flex: none;
    background: var(--border);
}

/* Too narrow for one row (set by app.js): logo and actions on top, the
   toolbar underneath */
.app-header.is-stacked {
    flex-wrap: wrap;
    row-gap: 2px;
    padding: 5px 6px;
}

.is-stacked .header-sep {
    display: none;
}

.is-stacked .toolbar {
    order: 1;
    flex: 1 1 100%;
}

.is-stacked .header-actions {
    padding-left: 0;
}

/* --- Top bar actions ----------------------------------------------------- */

.header-actions {
    margin-left: auto;
    flex: none;
    display: flex;
    align-items: center;
    gap: 8px;
    padding-left: 8px;
}

.theme-toggle .icon-sun {
    display: none;
}

:root[data-theme="dark"] .theme-toggle .icon-sun {
    display: block;
}

:root[data-theme="dark"] .theme-toggle .icon-moon {
    display: none;
}

.btn-export {
    padding: 0 14px 0 12px;
    border-radius: 11px;
}

/* Save state: an icon that grows leftwards into a pill on hover or focus,
   without moving anything else in the bar */
.save-status {
    position: relative;
    width: 32px;
    height: 32px;
    flex: none;
}

.save-pill {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 1;
    height: 32px;
    min-width: 32px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border: 1px solid transparent;
    border-radius: 999px;
    background: transparent;
    color: var(--text-control);
    font-size: 13px;
    white-space: nowrap;
    transition: padding 0.15s ease, background-color 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}

.save-pill:hover,
.save-pill:focus-visible {
    padding: 0 12px;
    background: var(--surface);
    border-color: var(--border-strong);
    box-shadow: var(--shadow-pill);
}

.save-detail {
    display: none;
    align-items: center;
    gap: 8px;
}

.save-pill:hover .save-detail,
.save-pill:focus-visible .save-detail,
.save-status[data-state="error"] .save-detail {
    display: flex;
}

.file-name {
    max-width: 220px;
    overflow: hidden;
    text-overflow: ellipsis;
    font-weight: 500;
}

.file-sep {
    width: 3px;
    height: 3px;
    flex: none;
    border-radius: 50%;
    background: var(--dot);
}

.save-label {
    color: var(--muted);
}

.save-icon {
    display: none;
    color: var(--accent);
}

.save-status[data-state="saved"] .save-icon-saved,
.save-status[data-state="pending"] .save-icon-pending,
.save-status[data-state="error"] .save-icon-error {
    display: block;
}

.save-track {
    stroke: var(--track);
}

.save-icon-pending {
    animation: spin 0.9s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* Errors stay expanded, take their own room and drop the file name */
.save-status[data-state="error"] {
    width: auto;
}

.save-status[data-state="error"] .save-pill {
    position: static;
    padding: 0 12px;
    background: var(--danger-bg);
    border-color: var(--danger-border);
    box-shadow: none;
    color: var(--danger-text);
    font-weight: 500;
}

.save-status[data-state="error"] .file-name,
.save-status[data-state="error"] .file-sep {
    display: none;
}

.save-status[data-state="error"] .save-label {
    color: inherit;
}

.save-status[data-state="error"] .save-icon-error {
    color: var(--danger);
}

/* --- Document card ------------------------------------------------------- */

.card {
    position: relative;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-card);
}

.panes {
    --split: 50%;
    flex: 1;
    min-height: 0;
    display: flex;
}

.pane {
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.pane-preview {
    flex: 0 0 var(--split);
    background: var(--surface);
}

.pane-editor {
    flex: 1 1 0;
    background: var(--surface-sunken);
}

/* View modes */
.app[data-view="edit"] .pane-preview,
.app[data-view="edit"] .splitter,
.app[data-view="preview"] .pane-editor,
.app[data-view="preview"] .splitter,
.app[data-view="edit"] .sync-toggle,
.app[data-view="preview"] .sync-toggle {
    display: none;
}

.app[data-view="preview"] .pane-preview {
    flex: 1 1 0;
}

/* --- Splitter ------------------------------------------------------------ */

.splitter {
    position: relative;
    z-index: 2;
    width: 1px;
    flex: none;
    background: var(--splitter);
    cursor: col-resize;
    touch-action: none;
}

.splitter::before {
    /* Wider invisible hit area */
    content: "";
    position: absolute;
    inset: 0 -6px;
}

.splitter-handle {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 12px;
    height: 40px;
    display: grid;
    place-items: center;
    transform: translate(-50%, -50%);
    border: 1px solid var(--border-strong);
    border-radius: 999px;
    background: var(--surface);
    box-shadow: 0 2px 6px -2px rgba(17, 24, 26, 0.18);
    transition: border-color 0.15s ease;
}

.splitter-handle::before {
    content: "";
    width: 2px;
    height: 14px;
    border-radius: 2px;
    background: var(--dot);
    transition: background-color 0.15s ease;
}

.splitter:hover .splitter-handle,
.splitter:focus-visible .splitter-handle,
.is-resizing .splitter-handle {
    border-color: var(--accent);
}

.splitter:hover .splitter-handle::before,
.splitter:focus-visible .splitter-handle::before,
.is-resizing .splitter-handle::before {
    background: var(--accent);
}

.splitter:focus-visible {
    outline: none;
}

.is-resizing,
.is-resizing * {
    cursor: col-resize !important;
    user-select: none;
}

/* --- Status bar ---------------------------------------------------------- */

.statusbar {
    flex: none;
    height: 34px;
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 0 8px 0 16px;
    border-top: 1px solid var(--border-soft);
    background: var(--surface);
    font-size: 12px;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

.statusbar-end {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 14px;
}

/* Sync scroll switch */
.sync-toggle {
    height: 26px;
    padding: 0 6px;
    display: flex;
    align-items: center;
    gap: 8px;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--muted);
    font-size: 12px;
}

.sync-toggle:hover {
    background: var(--hover);
}

.switch {
    position: relative;
    width: 28px;
    height: 16px;
    flex: none;
    border-radius: 999px;
    background: var(--switch-off);
    transition: background-color 0.15s ease;
}

.switch::after {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #FFFFFF;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
    transition: transform 0.15s ease;
}

[aria-pressed="true"] > .switch {
    background: var(--accent);
}

[aria-pressed="true"] > .switch::after {
    transform: translateX(12px);
}

.font-size-control {
    display: flex;
    align-items: center;
    gap: 2px;
}

.font-size-control button {
    width: 26px;
    height: 26px;
    padding: 0;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 7px;
    background: transparent;
    color: var(--text-control);
}

.font-size-control button:hover:not([aria-disabled="true"]) {
    background: var(--hover);
}

.font-size-control button[aria-disabled="true"] {
    opacity: 0.38;
    cursor: default;
}

.font-size-value {
    min-width: 38px;
    text-align: center;
    color: var(--text-control);
}

/* --- Responsive ---------------------------------------------------------- */

@media (max-width: 860px) {
    html,
    body {
        overflow: auto;
    }

    .app {
        height: auto;
        min-height: 100vh;
        min-height: 100dvh;
    }

    .stage {
        flex: none;
        margin: 6px;
        padding: 6px;
        gap: 6px;
        border-radius: 18px;
    }

    .card {
        flex: none;
    }

    .panes {
        flex-direction: column;
    }

    .pane,
    .app[data-view] .pane-preview {
        flex: none;
        height: 70vh;
    }

    .app[data-view="split"] .pane-preview {
        border-bottom: 1px solid var(--border-soft);
    }

    .splitter {
        display: none;
    }

    .statusbar {
        overflow-x: auto;
        scrollbar-width: none;
    }

    .statusbar::-webkit-scrollbar {
        display: none;
    }
}

@media (max-width: 640px) {
    .statusbar {
        padding: 0 6px 0 12px;
        gap: 12px;
    }
}

@media (max-width: 400px) {
    .header-actions {
        gap: 4px;
    }

    .segmented-icon > button {
        width: 28px;
    }

    .btn-export {
        width: 36px;
        padding: 0;
    }

    .btn-export .btn-label {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        transition: none !important;
        scroll-behavior: auto !important;
    }

    .save-icon-pending {
        animation: none;
    }
}
