/* Reformat — design tokens + layout.
   Standalone app: no shared bootstrap, no external assets. */

:root {
    --bg: #f3f4f6;
    --panel: #ffffff;
    --inset: #f7f8fa;
    --border: #e5e7eb;
    --border-strong: #d1d5db;
    --text: #111827;
    --muted: #6b7280;
    --accent: #4f46e5;
    --accent-hover: #4338ca;
    --accent-weak: #eef2ff;
    --accent-text: #4338ca;
    --on-accent: #ffffff;
    --danger: #dc2626;
    --danger-weak: #fef2f2;
    --detect-text: #92600a;
    --detect-weak: #fdf4dc;
    --shadow: 0 1px 2px rgba(17, 24, 39, .06), 0 1px 3px rgba(17, 24, 39, .08);
    --shadow-menu: 0 8px 28px rgba(17, 24, 39, .16);
    --radius: 12px;
    --radius-sm: 8px;
    --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

:root[data-theme="dark"] {
    --bg: #0d0f13;
    --panel: #171a21;
    --inset: #11141a;
    --border: #262b36;
    --border-strong: #38404f;
    --text: #e6e8ec;
    --muted: #98a0ad;
    --accent: #818cf8;
    --accent-hover: #a5b4fc;
    --accent-weak: rgba(129, 140, 248, .14);
    --accent-text: #a5b4fc;
    --on-accent: #14162b;
    --danger: #f87171;
    --danger-weak: rgba(248, 113, 113, .12);
    --detect-text: #f5c563;
    --detect-weak: rgba(251, 191, 36, .12);
    --shadow: none;
    --shadow-menu: 0 8px 28px rgba(0, 0, 0, .5);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { margin: 0; padding: 0; }

body {
    background: var(--bg);
    color: var(--text);
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
    min-height: 100vh;
}

h1, h2, h3 { margin: 0; font-weight: 650; }

button { font: inherit; color: inherit; cursor: pointer; }

.spacer { flex: 1; }

.ic { width: 18px; height: 18px; flex: none; }

/* ---------- top bar ---------- */

.topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 20px;
    background: color-mix(in srgb, var(--panel) 85%, transparent);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.brand h1 { font-size: 17px; letter-spacing: -.01em; }

.brand-icon {
    display: grid;
    place-items: center;
    width: 30px;
    height: 30px;
    border-radius: 9px;
    background: var(--accent);
    color: var(--on-accent);
    flex: none;
}
.brand-icon .ic { width: 17px; height: 17px; }

.tagline { color: var(--muted); font-size: 13px; white-space: nowrap; }

.topbar-actions { margin-left: auto; display: flex; gap: 6px; }

/* ---------- layout ---------- */

.layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 20px;
    max-width: 1440px;
    margin: 0 auto;
    padding: 20px;
    align-items: start;
}

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

.pane-right {
    position: sticky;
    top: 71px;
    max-height: calc(100vh - 91px);
}

/* ---------- cards ---------- */

.card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.card-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
}

.card-head h2, .section-head h2 { font-size: 14px; }

.card-foot {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 7px 12px;
    border-top: 1px solid var(--border);
    min-height: 34px;
    flex-wrap: wrap;
}

.step-badge {
    display: grid;
    place-items: center;
    width: 22px;
    height: 22px;
    border-radius: 7px;
    background: var(--accent-weak);
    color: var(--accent-text);
    font-size: 12px;
    font-weight: 700;
    flex: none;
}

.stats { color: var(--muted); font-size: 12.5px; font-variant-numeric: tabular-nums; }

.hint-inline {
    color: var(--muted);
    font-size: 12px;
    margin-left: auto;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    text-align: right;
}
.hint-inline .ic { width: 14px; height: 14px; }

/* ---------- buttons ---------- */

.icon-btn {
    display: inline-grid;
    place-items: center;
    width: 30px;
    height: 30px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--muted);
}
.icon-btn:hover { background: var(--inset); color: var(--text); }
.icon-btn .ic { width: 17px; height: 17px; }
.icon-btn:disabled { opacity: .4; cursor: default; }
.icon-btn:disabled:hover { background: transparent; color: var(--muted); }

.ghost-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--panel);
    color: var(--text);
    font-size: 13px;
    white-space: nowrap;
}
.ghost-btn:hover { border-color: var(--border-strong); background: var(--inset); }
.ghost-btn .ic { width: 15px; height: 15px; color: var(--muted); }

.copy-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 13px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--accent);
    color: var(--on-accent);
    font-size: 13px;
    font-weight: 600;
}
.copy-btn:hover { background: var(--accent-hover); }
.copy-btn:disabled { opacity: .5; cursor: default; }
.copy-btn .ic { width: 15px; height: 15px; }
.copy-btn.copied { background: #059669; color: #fff; }

.add-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 11px;
    border: 1.5px dashed var(--border-strong);
    border-radius: var(--radius);
    background: transparent;
    color: var(--muted);
    font-size: 14px;
    font-weight: 550;
}
.add-btn:hover { border-color: var(--accent); color: var(--accent-text); background: var(--accent-weak); }
.add-btn .ic { width: 16px; height: 16px; }

/* ---------- input area ---------- */

.input-wrap { position: relative; display: flex; }

#inputArea {
    width: 100%;
    min-height: 170px;
    max-height: 45vh;
    resize: vertical;
    border: none;
    outline: none;
    background: transparent;
    color: var(--text);
    font: 13px/1.55 var(--mono);
    padding: 12px 14px;
    tab-size: 4;
}
#inputArea::placeholder { color: var(--muted); opacity: .8; }

.input-wrap.dragover .drop-overlay { display: flex; }
.drop-overlay {
    display: none;
    position: absolute;
    inset: 6px;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border: 2px dashed var(--accent);
    border-radius: var(--radius-sm);
    background: color-mix(in srgb, var(--accent-weak) 80%, transparent);
    color: var(--accent-text);
    font-weight: 600;
    pointer-events: none;
}

/* ---------- formats stack ---------- */

.section-head { display: flex; align-items: center; gap: 8px; padding: 0 2px; }

.stack-section { display: flex; flex-direction: column; gap: 12px; }

.stack { display: flex; flex-direction: column; gap: 10px; }
.stack:empty { display: none; }

.format-card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    transition: border-color .15s, opacity .15s;
}
.format-card.previewing { border-color: var(--accent); }
.format-card.after-preview { opacity: .45; }
.format-card.has-error { border-color: color-mix(in srgb, var(--danger) 55%, var(--border)); }
.format-card.drag-ghost { opacity: .35; }

.fmt-head {
    display: flex;
    align-items: center;
    gap: 7px;
    padding: 8px 10px;
    min-width: 0;
}

.drag-handle { color: var(--muted); cursor: grab; display: grid; place-items: center; padding: 2px 0; }
.drag-handle:active { cursor: grabbing; }
.drag-handle .ic { width: 15px; height: 15px; }

.fmt-num {
    display: grid;
    place-items: center;
    width: 20px;
    height: 20px;
    border-radius: 6px;
    background: var(--inset);
    color: var(--muted);
    font-size: 11.5px;
    font-weight: 700;
    flex: none;
}
.format-card.previewing .fmt-num { background: var(--accent); color: var(--on-accent); }

.fmt-icon { color: var(--accent-text); display: grid; place-items: center; }
.fmt-icon .ic { width: 17px; height: 17px; }

.fmt-title { font-size: 14px; font-weight: 600; white-space: nowrap; }

.detect-note {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 2px 8px;
    border-radius: 99px;
    background: var(--detect-weak);
    color: var(--detect-text);
    font-size: 11.5px;
    font-weight: 550;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.detect-note .ic { width: 12px; height: 12px; flex: none; }

.fmt-actions { display: flex; gap: 2px; margin-left: auto; flex: none; }
.fmt-actions .icon-btn { width: 27px; height: 27px; }
.fmt-actions .icon-btn .ic { width: 15px; height: 15px; }
.fmt-actions .icon-btn.active { background: var(--accent-weak); color: var(--accent-text); }
.fmt-actions .chev .ic { transition: transform .15s; }
.format-card.collapsed .fmt-actions .chev .ic { transform: rotate(-90deg); }

.fmt-body { border-top: 1px solid var(--border); padding: 12px; }
.format-card.collapsed .fmt-body { display: none; }

.fmt-controls {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
    gap: 10px 14px;
}

.ctl { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.ctl-label { font-size: 12px; font-weight: 600; color: var(--muted); }

.ctl input[type="text"], .ctl select {
    width: 100%;
    padding: 7px 9px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--inset);
    color: var(--text);
    font-size: 13.5px;
    outline: none;
}
.ctl input[type="text"]:focus, .ctl select:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-weak);
}
.ctl input.mono { font-family: var(--mono); font-size: 13px; }

.ctl-check {
    flex-direction: row;
    align-items: center;
    gap: 8px;
    padding-top: 20px;
    font-size: 13.5px;
}
.ctl-check input { accent-color: var(--accent); width: 15px; height: 15px; margin: 0; flex: none; }

.sep-pair { display: flex; gap: 6px; }
.sep-pair select { flex: 1; }
.sep-pair input { flex: 1; min-width: 60px; }

.fmt-foot {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    margin-top: 11px;
}

.fmt-hint {
    color: var(--muted);
    font-size: 12px;
    line-height: 1.45;
    flex: 1;
}

.redetect-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px 9px;
    border: 1px solid var(--border);
    border-radius: 99px;
    background: transparent;
    color: var(--muted);
    font-size: 12px;
    white-space: nowrap;
}
.redetect-btn:hover { color: var(--detect-text); border-color: var(--detect-text); background: var(--detect-weak); }
.redetect-btn .ic { width: 13px; height: 13px; }

.fmt-error {
    margin-top: 10px;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    background: var(--danger-weak);
    color: var(--danger);
    font-size: 12.5px;
}

/* suggestion / empty stack */

.suggest-card {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--panel);
    box-shadow: var(--shadow);
}
.suggest-msg { display: flex; align-items: flex-start; gap: 8px; color: var(--muted); font-size: 13.5px; }
.suggest-msg .ic { width: 16px; height: 16px; color: var(--detect-text); flex: none; margin-top: 2px; }
.suggest-msg strong { color: var(--text); }
.suggest-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- menus ---------- */

.menu-anchor { position: relative; }

.menu {
    position: absolute;
    right: 0;
    top: calc(100% + 6px);
    z-index: 40;
    min-width: 300px;
    max-width: min(380px, 92vw);
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-menu);
    padding: 6px;
    max-height: 60vh;
    overflow-y: auto;
}
.menu.menu-up { top: auto; bottom: calc(100% + 6px); left: 0; right: auto; }

.menu-item {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    width: 100%;
    padding: 9px 10px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    text-align: left;
}
.menu-item:hover { background: var(--inset); }
.menu-item .fmt-icon { margin-top: 1px; }
.mi-text { min-width: 0; flex: 1; }
.mi-name { font-size: 13.5px; font-weight: 600; display: flex; align-items: center; gap: 7px; }
.mi-blurb { font-size: 12px; color: var(--muted); line-height: 1.4; }
.mi-detect {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    color: var(--detect-text);
    font-size: 11px;
    font-weight: 600;
}
.mi-detect .ic { width: 12px; height: 12px; }

.menu-sep { height: 1px; background: var(--border); margin: 6px 4px; }
.menu-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 6px 10px 3px; }

.preset-row { display: flex; align-items: center; gap: 4px; }
.preset-row .menu-item { flex: 1; padding: 8px 10px; }
.preset-row .mi-name { font-weight: 550; }
.preset-row .icon-btn { flex: none; }

.preset-save { display: flex; gap: 6px; padding: 8px 6px 4px; }
.preset-save input {
    flex: 1;
    min-width: 0;
    padding: 6px 9px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--inset);
    color: var(--text);
    font-size: 13px;
    outline: none;
}
.preset-save input:focus { border-color: var(--accent); }
.preset-save .ghost-btn { flex: none; }

.menu-empty { padding: 8px 10px; color: var(--muted); font-size: 12.5px; }

/* ---------- result ---------- */

.result-card { flex: 1; min-height: 320px; max-height: 100%; }

.tabs {
    display: flex;
    gap: 2px;
    padding: 2px;
    background: var(--inset);
    border-radius: var(--radius-sm);
}
.tab {
    padding: 4px 12px;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--muted);
    font-size: 12.5px;
    font-weight: 600;
}
.tab.active { background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
.tab:disabled { opacity: .45; cursor: default; }

.preview-chip {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 12px;
    background: var(--accent-weak);
    color: var(--accent-text);
    font-size: 12.5px;
    font-weight: 550;
    border-bottom: 1px solid var(--border);
}
.preview-chip .ic { width: 14px; height: 14px; }
.preview-chip button {
    margin-left: auto;
    border: none;
    background: none;
    color: inherit;
    font-size: 12.5px;
    font-weight: 700;
    text-decoration: underline;
    padding: 0;
}

.error-banner {
    padding: 9px 14px;
    background: var(--danger-weak);
    color: var(--danger);
    font-size: 13px;
    border-bottom: 1px solid var(--border);
}
.error-banner strong { font-weight: 700; }

.result-body { position: relative; flex: 1; display: flex; min-height: 200px; overflow: hidden; }

.result-raw {
    flex: 1;
    margin: 0;
    padding: 12px 14px;
    overflow: auto;
    font: 13px/1.55 var(--mono);
    tab-size: 4;
    white-space: pre;
    color: var(--text);
    outline: none;
}

.result-render { flex: 1; padding: 14px; overflow: auto; }

.result-empty {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    color: var(--muted);
    text-align: center;
    padding: 20px;
    background: var(--panel);
}
.result-empty .ic { width: 26px; height: 26px; opacity: .5; }
.result-empty p { margin: 0; font-size: 13.5px; max-width: 260px; }

/* rendered tables / HTML */

.render-scope table { border-collapse: collapse; font-size: 13.5px; max-width: 100%; }
.render-scope th, .render-scope td {
    border: 1px solid var(--border);
    padding: 7px 12px;
    text-align: left;
    vertical-align: top;
}
.render-scope th { background: var(--inset); font-weight: 650; }
.render-scope tr:nth-child(even) td { background: color-mix(in srgb, var(--inset) 55%, transparent); }

/* ---------- help dialog ---------- */

.help-dialog {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--panel);
    color: var(--text);
    padding: 0;
    width: min(560px, calc(100vw - 32px));
    box-shadow: var(--shadow-menu);
}
.help-dialog::backdrop { background: rgba(0, 0, 0, .45); backdrop-filter: blur(2px); }

.help-head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
}
.help-head h2 { font-size: 15px; }
.help-head > .ic { color: var(--accent-text); width: 18px; height: 18px; }

.help-body { padding: 6px 20px 18px; font-size: 13.5px; line-height: 1.6; }
.help-body h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 16px 0 6px; }
.help-body ol { padding-left: 20px; margin: 12px 0; }
.help-body ol li { margin-bottom: 8px; }
.help-body ul { list-style: none; padding: 0; margin: 0; }
.help-body ul li { display: flex; gap: 9px; align-items: flex-start; margin-bottom: 8px; color: var(--muted); }
.help-body ul li .help-ic { flex: none; margin-top: 3px; color: var(--accent-text); }
.help-body ul li .help-ic .ic { width: 15px; height: 15px; }
.help-body strong, .help-body em { color: var(--text); }
.help-body kbd {
    font: 11.5px var(--mono);
    padding: 1px 5px;
    border: 1px solid var(--border-strong);
    border-bottom-width: 2px;
    border-radius: 5px;
    background: var(--inset);
}

/* ---------- responsive ---------- */

@media (max-width: 960px) {
    .layout { grid-template-columns: 1fr; padding: 14px; gap: 16px; }
    .pane-right { position: static; max-height: none; }
    .result-card { max-height: none; }
    .result-raw { max-height: 50vh; }
    .tagline { display: none; }
    #inputArea { min-height: 130px; }
    .ctl-check { padding-top: 0; }
    .hint-inline { display: none; }
    .hint-inline.privacy { display: inline-flex; }
}

@media (max-width: 480px) {
    .ghost-btn span.btn-label { display: none; }
    .card-head { gap: 6px; }
}

@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; }
}
