:root {
    --bg: #f1f2ef;
    --panel: #fbfbf8;
    --ink: #191a17;
    --muted: #6f746b;
    --line: #d9dcd4;
    --accent: #1f4d3d;
    --accent-ink: #fff;
    --stage: #dedfd8;

    --shadow:
        0 24px 70px
        rgba(25, 26, 23, .13);

    --radius: 14px;
}


/* ================================================================
   BASE
================================================================ */

* {
    box-sizing: border-box;
}

html,
body {
    min-height: 100%;
}

body {
    margin: 0;
    color: var(--ink);
    background: var(--bg);

    font-family:
        Inter,
        ui-sans-serif,
        system-ui,
        -apple-system,
        BlinkMacSystemFont,
        "Segoe UI",
        sans-serif;
}

button,
input,
select {
    font: inherit;
}

button {
    color: inherit;
}


/* ================================================================
   HEADER
================================================================ */

.app-header {
    min-height: 86px;

    padding: 18px 28px;

    display: flex;
    align-items: center;
    justify-content: space-between;

    gap: 24px;

    border-bottom:
        1px solid var(--line);

    background:
        rgba(251, 251, 248, .9);

    backdrop-filter: blur(12px);

    position: sticky;
    top: 0;

    z-index: 20;
}


.eyebrow {
    margin: 0 0 3px;

    color: var(--accent);

    font-size: 11px;
    font-weight: 800;
    letter-spacing: .12em;

    text-transform: uppercase;
}


h1,
h2,
p {
    margin-top: 0;
}


h1 {
    margin-bottom: 0;

    font-size: 23px;
    letter-spacing: -.025em;
}


h2 {
    margin: 0;

    font-size: 15px;
    letter-spacing: -.01em;
}


.header-actions {
    display: flex;
    gap: 10px;
}


.button,
.icon-button {
    border: 1px solid transparent;
    border-radius: 9px;

    cursor: pointer;

    transition:
        transform .12s ease,
        background .12s ease,
        border-color .12s ease;
}


.button:active,
.icon-button:active {
    transform: translateY(1px);
}


.button {
    padding: 10px 14px;
    font-weight: 700;
}


.button.primary {
    background: var(--accent);
    color: var(--accent-ink);
}


.button.secondary {
    background: transparent;
    border-color: var(--line);
}


.button span {
    margin-left: 4px;
    opacity: .76;
}


/* ================================================================
   APPLICATION LAYOUT
================================================================ */

.app-shell {
    display: grid;

    /*
     * The control panel now contracts gradually as the viewport
     * narrows instead of remaining close to 390px wide.
     *
     * minmax(0, 1fr) is important: it allows the workspace column
     * to actually shrink rather than forcing horizontal overflow.
     */
    grid-template-columns:
        clamp(320px, 27vw, 390px)
        minmax(0, 1fr);

    min-height: calc(100vh - 86px);
}


.controls-panel {
    padding: 20px;

    background: var(--panel);

    border-right:
        1px solid var(--line);

    overflow: auto;

    max-height:
        calc(100vh - 86px);

    position: sticky;
    top: 86px;
}


/* ================================================================
   CONTROL SECTIONS
================================================================ */

.control-section {
    padding: 18px 0;

    border-bottom:
        1px solid var(--line);
}


.control-section:first-child {
    padding-top: 4px;
}


.control-section:last-child {
    border-bottom: 0;
}


.section-heading {
    display: flex;
    justify-content: space-between;
    align-items: center;

    gap: 12px;

    margin-bottom: 14px;
}


.section-heading > div:first-child {
    display: flex;
    align-items: baseline;

    gap: 8px;
}


.step {
    color: var(--accent);

    font-size: 10px;
    font-weight: 900;
    letter-spacing: .08em;
}


.muted {
    color: var(--muted);
    font-size: 12px;
}


/* ================================================================
   FORM CONTROLS
================================================================ */

label {
    display: block;
    margin-bottom: 12px;
}


label > span:first-child {
    display: block;

    margin-bottom: 6px;

    color: #4d514a;

    font-size: 12px;
    font-weight: 700;
}


label small {
    color: var(--muted);
    font-weight: 500;
}


input,
select {
    width: 100%;
    height: 38px;

    padding: 0 10px;

    border:
        1px solid #ccd0c7;

    border-radius: 8px;

    color: var(--ink);
    background: #fff;

    outline: none;
}


input:focus,
select:focus {
    border-color: #7a9186;

    box-shadow:
        0 0 0 3px
        rgba(31, 77, 61, .08);
}


.grid {
    display: grid;
    gap: 10px;
}


.grid.two {
    grid-template-columns:
        minmax(0, 1fr)
        minmax(0, 1fr);
}


/* ================================================================
   FILE UPLOAD
================================================================ */

.file-drop {
    padding: 13px;

    border:
        1px dashed #a9aea4;

    border-radius: 10px;

    cursor: pointer;

    background: #f6f7f3;
}


.file-drop:hover {
    border-color: var(--accent);
}


.file-drop-title {
    display: block;

    margin: 0 !important;

    color: var(--ink) !important;

    font-size: 13px !important;
}


.file-drop-subtitle {
    display: block;

    margin: 3px 0 0 !important;

    color: var(--muted) !important;

    font-size: 11px !important;
    font-weight: 500 !important;
}


.file-drop input {
    position: absolute;

    width: 1px;
    height: 1px;

    opacity: 0;

    pointer-events: none;
}


.file-drop.dragging {
    border-color: var(--accent);

    background: #eef3f0;

    box-shadow:
        0 0 0 3px
        rgba(31, 77, 61, .08);
}


/* ================================================================
   SPREADSHEET HELP
================================================================ */

.spreadsheet-help {
    margin: -3px 0 12px;

    border:
        1px solid var(--line);

    border-radius: 8px;

    background: #f7f8f5;
}


.spreadsheet-help summary {
    display: flex;
    align-items: center;

    gap: 7px;

    padding: 8px 10px;

    color: var(--accent);

    cursor: pointer;

    font-size: 11px;
    font-weight: 800;

    user-select: none;

    list-style: none;
}


.spreadsheet-help summary::-webkit-details-marker {
    display: none;
}


.spreadsheet-help summary::before {
    content: "?";

    width: 15px;
    height: 15px;

    display: inline-grid;
    place-items: center;

    flex: 0 0 15px;

    border:
        1px solid currentColor;

    border-radius: 50%;

    font-size: 9px;
    font-weight: 900;
    line-height: 1;
}


.spreadsheet-help summary::after {
    content: "›";

    margin-left: auto;

    color: var(--muted);

    font-size: 15px;
    line-height: 1;

    transform: rotate(90deg);

    transition:
        transform .12s ease;
}


.spreadsheet-help[open] summary {
    border-bottom:
        1px solid var(--line);
}


.spreadsheet-help[open] summary::after {
    transform: rotate(-90deg);
}


.spreadsheet-help-body {
    padding: 10px;

    color: var(--muted);

    font-size: 11px;
    line-height: 1.5;
}


.spreadsheet-help-body p {
    margin: 0 0 9px;
}


.spreadsheet-help-body p:last-child {
    margin-bottom: 0;
}


.spreadsheet-help-body strong {
    color: #4d514a;
}


.spreadsheet-example {
    margin: 9px 0 10px;

    overflow-x: auto;

    border:
        1px solid var(--line);

    border-radius: 6px;

    background: #fff;
}


.spreadsheet-example table {
    width: 100%;

    border-collapse: collapse;

    white-space: nowrap;

    font-size: 10px;
}


.spreadsheet-example th,
.spreadsheet-example td {
    padding: 5px 7px;

    border-right:
        1px solid #eceee8;

    border-bottom:
        1px solid #eceee8;

    text-align: left;
}


.spreadsheet-example th:last-child,
.spreadsheet-example td:last-child {
    border-right: 0;
}


.spreadsheet-example tr:last-child td {
    border-bottom: 0;
}


.spreadsheet-example th {
    color: #4d514a;
    background: #f5f6f2;

    font-weight: 800;
}


/* ================================================================
   UPLOAD STATUS
================================================================ */

.upload-status-row {
    min-height: 22px;

    margin: -2px 0 12px;

    display: flex;
    align-items: center;
    justify-content: space-between;

    gap: 10px;
}


.upload-status {
    min-width: 0;

    overflow: hidden;

    text-overflow: ellipsis;
    white-space: nowrap;
}


.upload-status[data-type="success"] {
    color: var(--accent);
    font-weight: 700;
}


.upload-status[data-type="error"] {
    color: #9a2f2f;
    font-weight: 700;
}


/* ================================================================
   SMALL BUTTONS
================================================================ */

.text-button {
    appearance: none;

    border: 0;

    padding: 2px 0;

    color: var(--accent);
    background: none;

    font-size: 11px;
    font-weight: 800;

    cursor: pointer;
}


.mini-actions {
    display: flex;
    gap: 10px;
}


/* ================================================================
   RANGE CONTROL
================================================================ */

.range-row {
    display: grid;

    grid-template-columns:
        minmax(0, 1fr)
        44px;

    align-items: center;

    gap: 10px;
}


.range-row input[type="range"] {
    height: auto;

    padding: 0;

    border: 0;

    box-shadow: none;

    accent-color: var(--accent);
}


.range-row output {
    color: var(--muted);

    font-size: 12px;

    text-align: right;
}


/* ================================================================
   HELPERS
================================================================ */

.hidden {
    display: none !important;
}


.sr-only {
    position: absolute;

    width: 1px;
    height: 1px;

    padding: 0;
    margin: -1px;

    overflow: hidden;

    clip: rect(0, 0, 0, 0);

    white-space: nowrap;

    border: 0;
}


/* ================================================================
   RECORDS
================================================================ */

.records-section {
    padding-bottom: 0;
}


.search-field {
    margin-bottom: 8px;
}


.record-list {
    max-height: 280px;

    overflow: auto;

    border:
        1px solid var(--line);

    border-radius: 9px;

    background: #fff;
}


.record-item {
    display: grid;

    grid-template-columns:
        24px
        minmax(0, 1fr);

    gap: 8px;

    align-items: start;

    padding: 9px 10px;

    border-bottom:
        1px solid #eceee8;

    cursor: pointer;
}


.record-item:last-child {
    border-bottom: 0;
}


.record-item:hover {
    background: #f7f8f5;
}


.record-item.active {
    background: #eef3f0;
}


.record-item input {
    width: 15px;
    height: 15px;

    margin: 3px 0 0;

    accent-color: var(--accent);
}


.record-main {
    min-width: 0;
}


.record-title {
    overflow: hidden;

    font-size: 12px;
    font-weight: 750;

    text-overflow: ellipsis;
    white-space: nowrap;
}


.record-subtitle {
    margin-top: 2px;

    overflow: hidden;

    color: var(--muted);

    font-size: 10.5px;

    text-overflow: ellipsis;
    white-space: nowrap;
}


.record-list-empty {
    padding: 20px 14px;

    color: var(--muted);

    font-size: 11px;
    line-height: 1.45;

    text-align: center;
}


/* ================================================================
   WORKSPACE
================================================================ */

.workspace {
    /*
     * Required for a grid/flex child containing something with
     * an intrinsic width. Without this, it can resist shrinking.
     */
    min-width: 0;

    display: flex;
    flex-direction: column;

    padding: 24px 30px 30px;
}


.workspace-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;

    margin-bottom: 14px;
}


.preview-nav {
    display: flex;
    gap: 6px;
}


.icon-button {
    width: 34px;
    height: 34px;

    background: var(--panel);

    border-color: var(--line);
}


/* ================================================================
   RESPONSIVE PREVIEW STAGE
================================================================ */

.preview-stage {
    /*
     * min-width: 0 ensures the stage itself is allowed to shrink
     * inside the workspace.
     */
    min-width: 0;

    flex: 1;

    min-height: 520px;

    /*
     * Padding now contracts with viewport width.
     * This saves useful preview space before the mobile breakpoint.
     */
    padding:
        clamp(18px, 2.5vw, 44px);

    display: grid;
    place-items: center;

    overflow: hidden;

    border-radius: var(--radius);

    background:
        radial-gradient(
            circle at 25% 20%,
            rgba(255, 255, 255, .55),
            transparent 38%
        ),
        var(--stage);
}


/* ================================================================
   ENVELOPE PREVIEW
================================================================ */

.envelope-preview {
    --env-ratio: 1.3785310734;

    --preview-font:
        Georgia,
        'Times New Roman',
        serif;

    --preview-font-size: 16pt;
    --preview-line-height: 1.35;
    --preview-letter-spacing: 0pt;
    --preview-font-weight: 400;

    --offset-x: 0in;
    --offset-y: 0in;

    --block-width: 86%;

    position: relative;

    /*
     * IMPORTANT:
     *
     * Previously:
     *     width: min(72vw, 830px);
     *
     * That based the envelope on the full browser viewport even
     * though the preview only occupies the space remaining after
     * the control panel.
     *
     * It is now based on the preview-stage's actual available width.
     *
     * It grows to at most 830px, but shrinks automatically to 100%
     * of the stage whenever there is less room.
     */
    width: min(100%, 830px);

    min-width: 0;

    aspect-ratio: var(--env-ratio);

    background: #fffef9;

    border:
        1px solid
        rgba(80, 80, 70, .12);

    box-shadow: var(--shadow);
}


/* ================================================================
   ADDRESS
================================================================ */

.address-block {
    position: absolute;

    left: 50%;
    top: 50%;

    width: var(--block-width);

    transform:
        translate(
            calc(-50% + var(--offset-x)),
            calc(-50% + var(--offset-y))
        );

    color: #10100f;

    font-family: var(--preview-font);
    font-size: var(--preview-font-size);
    font-weight: var(--preview-font-weight);
    line-height: var(--preview-line-height);
    letter-spacing: var(--preview-letter-spacing);

    text-align: center;

    /*
     * JavaScript explicitly changes this to uppercase
     * when the checkbox is enabled.
     */
    text-transform: none;
}


.address-line {
    min-height: 1em;
}


.address-line:empty {
    display: none;
}


.empty-preview-message {
    color: #8a8d86;

    font-family:
        Inter,
        ui-sans-serif,
        system-ui,
        -apple-system,
        BlinkMacSystemFont,
        "Segoe UI",
        sans-serif;

    font-size: 11pt;
    font-weight: 600;
    letter-spacing: 0;
}


/* ================================================================
   TYPOGRAPHY TOGGLES
================================================================ */

.checkbox-control {
    width: fit-content;

    margin: 2px 0 0;

    display: flex;
    align-items: center;

    gap: 8px;

    cursor: pointer;
}


.checkbox-control input[type="checkbox"] {
    width: 16px;
    height: 16px;

    padding: 0;
    margin: 0;

    accent-color: var(--accent);

    cursor: pointer;
}


.checkbox-control > span:first-child {
    display: inline;

    margin: 0;
}


/* ================================================================
   PRINTING GUIDANCE
================================================================ */

.print-guidance {
    margin-top: 14px;

    color: var(--muted);

    font-size: 11px;
    line-height: 1.45;
}


/* ================================================================
   DISABLED CONTROLS
================================================================ */

button:disabled,
input:disabled,
select:disabled {
    opacity: .48;
    cursor: not-allowed;
}


/* ================================================================
   PRINT ROOT
================================================================ */

#print-root {
    display: none;
}


/* ================================================================
   INTERMEDIATE WIDTHS
================================================================ */

@media (max-width: 1400px) {

    .workspace {
        padding:
            22px
            clamp(18px, 2vw, 26px)
            26px;
    }

}


/* ================================================================
   TABLET / NARROW DESKTOP
================================================================ */

@media (max-width: 900px) {

    .app-header {
        position: static;
    }


    .app-shell {
        grid-template-columns: 1fr;
    }


    .controls-panel {
        position: static;

        max-height: none;

        border-right: 0;

        border-bottom:
            1px solid var(--line);
    }


    .workspace {
        padding: 20px;
    }


    .preview-stage {
        min-height: 430px;

        padding:
            clamp(16px, 4vw, 24px);
    }


    /*
     * Still capped at 830px, but on a small viewport the envelope
     * simply becomes as wide as the available stage.
     */
    .envelope-preview {
        width: min(100%, 830px);
    }

}


/* ================================================================
   PHONE
================================================================ */

@media (max-width: 620px) {

    .app-header {
        padding: 16px 18px;

        flex-direction: column;
        align-items: stretch;

        gap: 12px;
    }


    .header-actions {
        width: 100%;
    }


    .header-actions .button {
        flex: 1;
    }


    .controls-panel {
        padding: 18px;
    }


    .workspace {
        padding: 16px;
    }


    .preview-stage {
        min-height: 300px;
        padding: 14px;
    }


    .grid.two {
        grid-template-columns: 1fr;
    }


    .spreadsheet-example {
        max-width: 100%;
    }

}


/* ================================================================
   PRINT
================================================================ */

@media print {

    html,
    body {
        margin: 0 !important;
        padding: 0 !important;

        background: #fff !important;
    }


    .no-print {
        display: none !important;
    }


    #print-root {
        display: block !important;
    }


    .print-page {
        position: relative;

        overflow: hidden;

        margin: 0 !important;
        padding: 0 !important;

        break-after: page;
        page-break-after: always;

        background: #fff;
    }


    .print-page:last-child {
        break-after: auto;
        page-break-after: auto;
    }


    .print-page .address-block {
        position: absolute;
    }

}
