:root {
    --bg-main: #1a1b1c;
    --bg-panel: #252627;
    --bg-elevated: #2d2e30;
    --bg-card: #2d2e30;
    --border: rgba(255, 255, 255, 0.08);
    --border-strong: rgba(255, 255, 255, 0.15);
    --text-primary: #fdfbf7;
    --text-secondary: rgba(253, 251, 247, 0.7);
    --text-muted: rgba(253, 251, 247, 0.45);
    --accent: #f07727;
    --accent-hover: #ff8a3d;
    --header-h: 56px;
    --footer-h: 56px;
    --sidebar-w: 280px;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: var(--bg-main);
    color: var(--text-primary);
    font-family: 'Roboto', system-ui, sans-serif;
    font-size: 14px;
    -webkit-font-smoothing: antialiased;
}

/* Hide number spinners globally */
input[type='number']::-webkit-outer-spin-button,
input[type='number']::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
input[type='number'] {
    -moz-appearance: textfield;
}

/* Header */
#header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--header-h);
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    z-index: 10;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 16px;
}

.header-title {
    display: flex;
    align-items: baseline;
    gap: 10px;
}

.header-title .brand {
    color: var(--accent);
    font-weight: 700;
    font-size: 1.1rem;
    letter-spacing: 0.5px;
}

.header-title .app-title {
    font-size: 1rem;
    font-weight: 500;
    color: var(--text-primary);
}

.ai-badge {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    color: var(--accent);
    border-radius: 6px;
    padding: 4px 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    transition: background 0.15s;
}
.ai-badge:hover {
    background: rgba(240, 119, 39, 0.15);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

.message-count {
    color: var(--text-muted);
    font-size: 0.85rem;
    margin-right: 6px;
}

.btn-primary,
.btn-secondary {
    border: none;
    padding: 8px 14px;
    border-radius: 6px;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.875rem;
    font-weight: 500;
    transition:
        background 0.15s,
        opacity 0.15s;
}

.btn-primary {
    background: var(--accent);
    color: #fff;
}
.btn-primary:hover:not(:disabled) {
    background: var(--accent-hover);
}
.btn-primary:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.btn-secondary {
    background: var(--bg-elevated);
    color: var(--text-primary);
    border: 1px solid var(--border);
}
.btn-secondary:hover:not(:disabled) {
    background: var(--bg-card);
}
.btn-secondary:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Layout */
#app-layout {
    position: fixed;
    top: var(--header-h);
    bottom: var(--footer-h);
    left: 0;
    right: 0;
    display: flex;
}

#sidebar {
    width: var(--sidebar-w);
    background: var(--bg-panel);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    padding: 16px;
}

.sidebar-section {
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
}
.sidebar-section:last-child {
    border-bottom: none;
}

.section-label {
    display: block;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-muted);
    margin-bottom: 10px;
}

.field-row {
    display: flex;
    gap: 10px;
    margin-bottom: 10px;
}
.field-row:last-child {
    margin-bottom: 0;
}

.field {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.field label {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.field input[type='number'],
.field input[type='text'],
.select-field {
    background: var(--bg-main);
    border: 1px solid var(--border);
    color: var(--text-primary);
    padding: 7px 10px;
    border-radius: 5px;
    font-family: inherit;
    font-size: 0.875rem;
    outline: none;
    width: 100%;
}
.field input:focus,
.select-field:focus {
    border-color: var(--accent);
}

.select-field {
    appearance: none;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'><path d='M1 1l4 4 4-4' stroke='%23aaa' stroke-width='1.5' fill='none'/></svg>");
    background-repeat: no-repeat;
    background-position: right 10px center;
    padding-right: 28px;
}

/* Drop zones */
.drop-zone {
    position: relative;
    border: 2px dashed var(--border-strong);
    border-radius: 8px;
    padding: 18px 10px;
    text-align: center;
    cursor: pointer;
    transition:
        border-color 0.15s,
        background 0.15s;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    color: var(--text-secondary);
    font-size: 0.8rem;
}
.drop-zone.small {
    padding: 10px;
}
.drop-zone:hover,
.drop-zone.drag-over {
    border-color: var(--accent);
    background: rgba(240, 119, 39, 0.08);
    color: var(--text-primary);
}
.drop-zone svg {
    color: var(--text-muted);
}
.drop-zone input[type='file'] {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
}

.hint {
    color: var(--text-muted);
    font-size: 0.72rem;
    margin: 6px 0 0 0;
}

.layout-info {
    font-size: 0.82rem;
    color: var(--text-secondary);
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.layout-info span {
    color: var(--text-muted);
    margin-right: 6px;
}
.layout-info b {
    color: var(--text-primary);
    font-weight: 500;
}

/* Preview */
#preview-area {
    flex: 1;
    position: relative;
    overflow: auto;
    background: #111;
    display: flex;
    flex-direction: column;
}

.preview-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    text-align: center;
    gap: 6px;
}
.preview-empty p {
    margin: 4px 0;
}

#preview-container {
    flex: 1;
    display: flex;
    flex-direction: column;
}

#page-nav {
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    padding: 8px 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    flex-shrink: 0;
}

.btn-nav {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    color: var(--text-primary);
    padding: 6px 12px;
    border-radius: 5px;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.85rem;
}
.btn-nav:hover:not(:disabled) {
    background: var(--bg-card);
}
.btn-nav:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

#page-indicator {
    color: var(--text-secondary);
    font-size: 0.9rem;
    min-width: 120px;
    text-align: center;
}

#sheet-wrapper {
    flex: 1;
    overflow: auto;
    padding: 30px;
    display: flex;
    align-items: flex-start;
    justify-content: center;
}

.sheet {
    background: #fff;
    color: #111;
    position: relative;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.6);
}

.sheet .margin-frame {
    position: absolute;
    border: 1px dashed rgba(0, 0, 0, 0.15);
    pointer-events: none;
}

.sheet .insert {
    position: absolute;
    border: 1px solid rgba(0, 0, 0, 0.35);
    display: flex;
    flex-direction: column;
    align-items: center;
    /* Explicit spacer divs (.gap) control vertical spacing — not flex justify */
    overflow: hidden;
    text-align: center;
    color: #111;
    line-height: 1;
}

.sheet .insert .gap {
    flex-shrink: 0;
    width: 100%;
}

.sheet .insert .line {
    /* Prevent CSS auto-wrap — the fit algorithm controls line breaks */
    white-space: nowrap;
    /* line-height set inline per-insert using the measured fontBoundingBox */
    display: block;
    flex-shrink: 0;
}

.sheet .insert.shrunk {
    outline: 1px solid rgba(240, 119, 39, 0.5);
}

/* Footer */
#footer-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--footer-h);
    background: var(--bg-panel);
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 14px;
    padding: 0 20px;
    z-index: 10;
}

.slider-label {
    color: var(--text-muted);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    font-weight: 600;
}

#font-size-slider {
    flex: 0 1 640px;
    appearance: none;
    height: 4px;
    border-radius: 2px;
    background: var(--bg-elevated);
    outline: none;
}
#font-size-slider::-webkit-slider-thumb {
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: 2px solid #1a1b1c;
    transition: transform 0.1s;
}
#font-size-slider::-webkit-slider-thumb:hover {
    transform: scale(1.15);
}
#font-size-slider::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: 2px solid #1a1b1c;
}

.size-btn {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    color: var(--text-primary);
    width: 28px;
    height: 28px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 1rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
}
.size-btn:hover {
    background: var(--bg-card);
}

.size-value {
    color: var(--text-primary);
    font-size: 0.9rem;
    font-weight: 500;
    font-variant-numeric: tabular-nums;
    min-width: 56px;
    text-align: left;
}

/* Preview area drag-over highlight (accepts messages or font files) */
#preview-area.drag-over {
    outline: 3px dashed var(--accent);
    outline-offset: -12px;
    background: #181818;
}
#preview-area.drag-over::before {
    content: 'Drop messages file (.txt / .csv / .docx) or font (.ttf / .otf / .woff)';
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--accent);
    font-size: 1rem;
    font-weight: 500;
    pointer-events: none;
    z-index: 5;
    background: rgba(26, 27, 28, 0.7);
}
