/* =============================================================
   m5.santrancisco.net — editor styles
   ============================================================= */

:root {
    --bg:            #F5F4EF;
    --surface:       #FFFFFF;
    --surface-alt:   #FAFAF7;
    --border:        #E5E1D7;
    --border-strong: #C9C3B6;
    --text:          #1A1A18;
    --text-muted:    #5F5D57;
    --accent:        #1F4B99;
    --accent-hover:  #163B7C;
    --success:       #1F7A4A;
    --danger:        #B7332A;

    --device-bg:     #F7F5EF; /* off-white e-ink */
    --device-ink:    #1A1A18;
    --device-frame:  #22201C;

    --radius-sm: 6px;
    --radius:    10px;
    --radius-lg: 14px;

    --shadow-sm: 0 1px 2px rgba(0,0,0,0.06);
    --shadow:    0 6px 20px rgba(0,0,0,0.10);
    --shadow-lg: 0 16px 40px rgba(0,0,0,0.18);

    --tap: 40px;
}

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

html, body {
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

button, input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

code {
    font-family: 'JetBrains Mono', 'SF Mono', 'Menlo', 'Consolas', monospace;
    font-size: 0.9em;
    background: var(--surface-alt);
    padding: 0.1em 0.35em;
    border-radius: 4px;
    border: 1px solid var(--border);
}

/* =========================================================
   App shell
   ========================================================= */

.app {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.625rem 1rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    position: sticky;
    top: 0;
    z-index: 20;
    flex-wrap: wrap;
}

.brand { display: inline-flex; align-items: center; gap: 0.625rem; }

.brand-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    background: var(--device-frame);
    color: #F7F5EF;
    font-family: 'JetBrains Mono', monospace;
    font-weight: 700;
    font-size: 0.875rem;
    border-radius: 8px;
    letter-spacing: 0.02em;
}

.brand-name {
    font-weight: 700;
    font-size: 1.0625rem;
    color: var(--text);
    letter-spacing: -0.01em;
}

.header-actions {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.main {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    padding: 1rem;
    flex: 1;
    align-items: start;
}

@media (max-width: 1080px) {
    .main { grid-template-columns: 1fr; }
}

.pane {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-sm);
}

.pane-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    background: var(--surface-alt);
    border-bottom: 1px solid var(--border);
}

.pane-head h3 {
    font-size: 0.8125rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    display: inline-flex;
    align-items: baseline;
    gap: 0.5rem;
}

.pane-head h3 .meta {
    text-transform: none;
    letter-spacing: 0;
    font-weight: 500;
    color: var(--text-muted);
    font-size: 0.75rem;
}

.meta {
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* =========================================================
   Editor
   ========================================================= */

.pane-editor { min-height: 620px; }

#editor {
    flex: 1;
    width: 100%;
    padding: 1rem 1.25rem;
    font-family: 'JetBrains Mono', 'SF Mono', 'Menlo', 'Consolas', monospace;
    font-size: 0.95rem;
    line-height: 1.55;
    color: var(--text);
    background: var(--surface);
    border: none;
    outline: none;
    resize: none;
    white-space: pre;
    overflow: auto;
    min-height: 560px;
    tab-size: 4;
}

#editor:focus { outline: none; }

/* =========================================================
   Preview
   ========================================================= */

.pane-preview .pane-head h3 { align-items: center; }

.preview-wrap {
    padding: 1rem;
    background:
      radial-gradient(circle at 20% 0%, rgba(31, 75, 153, 0.05), transparent 60%),
      linear-gradient(180deg, var(--surface) 0%, var(--surface-alt) 100%);
    display: flex;
    justify-content: center;
    align-items: flex-start;
    overflow: auto;
    min-height: 400px;
}

/* Scale wrapper keeps native 540×960 (portrait) while shrinking on narrow
   or short viewports. Size is set inline from JS in fitPreviewScale(). */
.preview-scale {
    width: 572px; /* 540 + 32 device padding */
    max-width: 100%;
    transform-origin: top left;
}

.device {
    width: 540px;
    height: 960px;
    background: var(--device-frame);
    border-radius: 24px;
    padding: 16px;
    box-shadow:
        var(--shadow-lg),
        inset 0 0 0 2px rgba(255,255,255,0.05);
    position: relative;
    box-sizing: content-box;
}

.device-frame {
    width: 100%;
    height: 100%;
    background: var(--device-bg);
    border-radius: 4px;
    position: relative;
    overflow: hidden;
    box-shadow: inset 0 0 0 1px rgba(0,0,0,0.08);
}

/* Screen matches M5Paper's own layout: header 44 + gap 14 + content + footer 44 */
.device-screen {
    position: absolute;
    inset: 0;
    color: var(--device-ink);
    display: flex;
    flex-direction: column;
}

.ds-header {
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    border-bottom: 1px solid rgba(26,26,24,0.20);
    background:
        linear-gradient(180deg, transparent 0px, transparent 40px, rgba(26,26,24,0.30) 40px, rgba(26,26,24,0.30) 43px);
    font-family: 'Inter', sans-serif;
}

.ds-title {
    font-size: 18px;
    font-weight: 600;
    color: var(--device-ink);
}

.ds-badge {
    font-size: 14px;
    color: rgba(26,26,24,0.55);
    font-family: 'JetBrains Mono', monospace;
    letter-spacing: 0.02em;
}

.ds-content {
    flex: 1;
    padding: 14px 20px 8px;
    overflow: hidden;
    position: relative;
    display: flex;
    align-items: flex-start;
    justify-content: center;
}

/* The pre element is where the notes are actually drawn.
   font-size is set inline from JS after the fit calculation. */
.ds-pre {
    font-family: 'JetBrains Mono', 'SF Mono', 'Menlo', 'Consolas', monospace;
    color: var(--device-ink);
    white-space: pre;
    line-height: 1.30;
    /* Placeholder — script.js overrides. */
    font-size: 18px;
    text-align: left;
    margin: 0;
    max-width: 100%;
    letter-spacing: 0;
}

.ds-footer {
    height: 44px;
    padding: 0 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-top: 1px solid rgba(26,26,24,0.20);
    font-family: 'JetBrains Mono', monospace;
    font-size: 14px;
    color: rgba(26,26,24,0.65);
}

.ds-hint {
    font-family: 'Inter', sans-serif;
    font-size: 13px;
    color: rgba(26,26,24,0.45);
}

@media (max-width: 460px) {
    .ds-hint { display: none; }
}

/* =========================================================
   Buttons
   ========================================================= */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.375rem;
    padding: 0.5rem 0.875rem;
    font-size: 0.875rem;
    font-weight: 600;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s, box-shadow 0.15s, transform 0.1s;
    min-height: var(--tap);
    white-space: nowrap;
}

.btn:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.btn:active:not(:disabled) { transform: scale(0.97); }

.btn-primary {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}
.btn-primary:not(:disabled):hover {
    background: var(--accent-hover);
    box-shadow: var(--shadow-sm);
}

.btn-ghost {
    background: transparent;
    color: var(--text);
    border-color: var(--border-strong);
}
.btn-ghost:hover {
    background: var(--surface-alt);
    border-color: var(--text-muted);
}

/* =========================================================
   Badges & status
   ========================================================= */

.badge {
    display: inline-flex;
    align-items: center;
    padding: 0.25rem 0.625rem;
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    border-radius: 999px;
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--text-muted);
    font-family: 'JetBrains Mono', monospace;
    line-height: 1;
    min-height: 22px;
}

.badge-neutral   { background: var(--surface-alt); color: var(--text-muted); }
.badge-dirty     { background: #FFF6E6; color: #8A5A00; border-color: #E9CB88; }
.badge-saving    { background: #E9F0FF; color: #1F4B99; border-color: #B8CCF4; }
.badge-saved     { background: #E7F5EC; color: var(--success); border-color: #A9D9BC; }
.badge-error     { background: #FBE7E4; color: var(--danger); border-color: #EDB4AC; }

/* =========================================================
   Password modal
   ========================================================= */

.modal {
    position: fixed;
    inset: 0;
    background: rgba(26, 26, 24, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    z-index: 100;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

.modal-panel {
    background: var(--surface);
    padding: 1.75rem 1.5rem;
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 400px;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
}

.modal-panel h2 {
    font-size: 1.25rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    letter-spacing: -0.01em;
}

.modal-desc {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-bottom: 1.25rem;
    line-height: 1.55;
}

.password-form {
    display: flex;
    flex-direction: column;
    gap: 0.625rem;
}

.password-form input {
    width: 100%;
    padding: 0.625rem 0.875rem;
    font-size: 0.9375rem;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
    min-height: var(--tap);
}

.password-form input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(31, 75, 153, 0.12);
}

.error-text {
    margin-top: 0.75rem;
    color: var(--danger);
    font-size: 0.8125rem;
    font-weight: 500;
}

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

.toast {
    position: fixed;
    top: 1rem;
    left: 50%;
    transform: translateX(-50%) translateY(-140%);
    background: var(--surface);
    color: var(--text);
    padding: 0.75rem 1.125rem;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    font-size: 0.875rem;
    font-weight: 500;
    z-index: 200;
    transition: transform 0.3s ease;
    border-left: 4px solid var(--accent);
    max-width: calc(100% - 2rem);
    text-align: center;
}

.toast.show { transform: translateX(-50%) translateY(0); }
.toast.success { border-left-color: var(--success); }
.toast.error   { border-left-color: var(--danger); }
