:root {
    /* ---- Spacing scale (4px base) ---- */
    --s-1: 4px;
    --s-2: 8px;
    --s-3: 12px;
    --s-4: 16px;
    --s-5: 20px;
    --s-6: 24px;
    --s-8: 32px;

    /* ---- Radius: organic, iOS 26-ish ---- */
    --r-sm: 12px;
    --r-md: 16px;
    --r-lg: 22px;
    --r-pill: 999px;

    /* ---- Typography ---- */
    --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI",
        "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
        "Liberation Mono", monospace;

    /* ---- iOS 26 Liquid Glass material tokens (light) ---- */
    --bg: radial-gradient(1200px 800px at 12% 8%, #c7d2fe 0%, transparent 45%),
          radial-gradient(1000px 700px at 88% 12%, #fbcfe8 0%, transparent 45%),
          radial-gradient(1100px 760px at 80% 92%, #bae6fd 0%, transparent 45%),
          radial-gradient(900px 640px at 18% 95%, #ddd6fe 0%, transparent 42%),
          linear-gradient(135deg, #eef2ff 0%, #fdf2f8 55%, #ecfeff 100%);

    --glass: rgba(255, 255, 255, 0.55);
    --glass-strong: rgba(255, 255, 255, 0.72);
    --glass-border: rgba(255, 255, 255, 0.65);
    --glass-inset: inset 0 1px 0 rgba(255, 255, 255, 0.75);
    --glass-shadow: 0 12px 40px -14px rgba(31, 41, 55, 0.28),
                    var(--glass-inset);
    --field-fill: rgba(255, 255, 255, 0.5);

    --text: #1a1d21;
    --text-strong: #0f1115;
    --muted: #5b6470;
    --muted-2: #8a93a1;
    --line: rgba(255, 255, 255, 0.55);
    --line-strong: rgba(255, 255, 255, 0.8);

    --primary: #16181d;
    --primary-hover: #2b2f37;
    --on-primary: #ffffff;

    --accent: #0d9488;
    --accent-ink: #0f766e;
    --accent-soft: rgba(13, 148, 136, 0.18);
    --accent-grad: linear-gradient(180deg, rgba(45, 212, 191, 0.95), rgba(13, 148, 136, 0.9));

    --warning: #b45309;

    --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

[data-theme="dark"] {
    --bg: radial-gradient(1200px 800px at 12% 8%, #4338ca 0%, transparent 45%),
          radial-gradient(1000px 700px at 88% 12%, #7c3aed 0%, transparent 45%),
          radial-gradient(1100px 760px at 80% 92%, #0e7490 0%, transparent 45%),
          radial-gradient(900px 640px at 18% 95%, #312e81 0%, transparent 42%),
          linear-gradient(135deg, #0b1020 0%, #150b2e 55%, #07131f 100%);

    --glass: rgba(30, 33, 45, 0.5);
    --glass-strong: rgba(44, 48, 63, 0.7);
    --glass-border: rgba(255, 255, 255, 0.14);
    --glass-inset: inset 0 1px 0 rgba(255, 255, 255, 0.14);
    --glass-shadow: 0 16px 48px -16px rgba(0, 0, 0, 0.6),
                    var(--glass-inset);
    --field-fill: rgba(12, 14, 20, 0.45);

    --text: #e7e9ee;
    --text-strong: #f4f6f9;
    --muted: #aab2c0;
    --muted-2: #7b8494;
    --line: rgba(255, 255, 255, 0.14);
    --line-strong: rgba(255, 255, 255, 0.26);

    --primary: #eceef2;
    --primary-hover: #ffffff;
    --on-primary: #16181d;

    --accent: #2dd4bf;
    --accent-ink: #5eead4;
    --accent-soft: rgba(45, 212, 191, 0.22);
    --accent-grad: linear-gradient(180deg, rgba(45, 212, 191, 0.95), rgba(13, 148, 136, 0.9));
}

@media (prefers-color-scheme: dark) {
    html:not([data-theme="light"]):not([data-theme="dark"]) {
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }

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

body {
    margin: 0;
    background: var(--bg);
    background-attachment: fixed;
    color: var(--text);
    font-family: var(--font-sans);
    font-size: 14px;
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
    transition: background var(--s-3) var(--ease), color var(--s-3) var(--ease);
}

button, textarea { font: inherit; color: inherit; }

button {
    transition: background var(--s-2) var(--ease),
        border-color var(--s-2) var(--ease),
        color var(--s-2) var(--ease),
        transform var(--s-2) var(--ease),
        box-shadow var(--s-2) var(--ease);
}

button:focus-visible,
textarea:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

button:active { transform: translateY(1px); }

/* shared frosted-material helper */
.top-bar, .model-bar, .input-panel, .result-panel,
.target-tab, .secondary-button, .feedback-button,
.target-more-menu, .result-target, .copy-button, .debug-panel, .primary-button {
    backdrop-filter: blur(28px) saturate(180%);
    -webkit-backdrop-filter: blur(28px) saturate(180%);
}

/* ---------- Shell ---------- */
.app-shell {
    display: grid;
    grid-template-rows: auto auto minmax(0, 1fr);
    width: min(1440px, 100%);
    height: 100vh;
    margin: 0 auto;
    padding: var(--s-6);
    gap: var(--s-5);
}

.top-bar,
.model-bar,
.input-panel,
.result-panel {
    border: 1px solid var(--glass-border);
    background: var(--glass);
    box-shadow: var(--glass-shadow);
}

.top-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s-5);
    min-height: 72px;
    border-radius: var(--r-lg);
    padding: var(--s-4) var(--s-6);
}

.top-bar__title { display: flex; align-items: baseline; gap: var(--s-3); }

.eyebrow {
    margin: 0 0 var(--s-2);
    color: var(--muted);
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.09em;
    text-transform: uppercase;
}

h1 {
    margin: 0;
    font-size: 22px;
    font-weight: 700;
    letter-spacing: -0.02em;
    line-height: 1.15;
}

h2 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    letter-spacing: -0.01em;
    line-height: 1.35;
}

.subtitle {
    margin: var(--s-2) 0 0;
    color: var(--muted);
    font-size: 14px;
    font-weight: 400;
}

.top-bar__right {
    display: flex;
    align-items: center;
    gap: var(--s-4);
}

.model-bar {
    position: relative;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: var(--s-4);
    border-radius: var(--r-lg);
    padding: var(--s-3) var(--s-5);
}

.model-bar__label {
    flex: 0 0 auto;
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
}

.model-bar .target-tabs { flex: 1 1 auto; }

.theme-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    flex: 0 0 auto;
    border: 1px solid var(--glass-border);
    border-radius: var(--r-md);
    background: var(--glass-strong);
    color: var(--muted);
    cursor: pointer;
}
.theme-toggle:hover { color: var(--text); border-color: var(--line-strong); }
.theme-toggle svg { width: 18px; height: 18px; }

/* ---------- Workbench ---------- */
.workbench {
    position: relative;
    z-index: 1;
    display: grid;
    grid-template-columns: 1fr 1fr;
    min-height: 0;
    gap: var(--s-5);
}

.input-panel,
.result-panel {
    display: flex;
    flex-direction: column;
    min-height: 0;
    border-radius: var(--r-lg);
    padding: var(--s-6);
    overflow-y: auto;
}

.panel-heading {
    display: flex;
    flex-direction: column;
    justify-content: center;
    min-height: 44px;
    padding-bottom: var(--s-3);
    border-bottom: 1px solid var(--line);
}

.section-block { margin-top: var(--s-3); }

.input-stack {
    display: flex;
    flex: 1 1 0;
    min-height: 0;
    flex-direction: column;
}

.field-label {
    display: block;
    margin-bottom: var(--s-3);
    color: var(--text);
    font-size: 13px;
    font-weight: 500;
}

textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    padding: var(--s-4);
    background: var(--field-fill);
    box-shadow: var(--glass-inset);
    outline: none;
    resize: none;
    color: var(--text);
    transition: border-color var(--s-2) var(--ease),
        box-shadow var(--s-2) var(--ease);
}
textarea::placeholder { color: var(--muted-2); }

textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft), var(--glass-inset);
}

#rawInput { flex: 1 1 auto; min-height: 0; overflow: auto; }

/* ---------- Target tabs (glass pills) ---------- */
.target-tabs {
    display: flex;
    flex-wrap: nowrap;
    gap: var(--s-2);
}

.target-more {
    position: relative;
    flex: 1 1 0;
    min-width: 0;
}

.target-more__button { width: 100%; }

.target-more-menu {
    position: absolute;
    z-index: 20;
    top: calc(100% + var(--s-2));
    right: 0;
    width: max-content;
    min-width: 180px;
    border: 1px solid var(--glass-border);
    border-radius: var(--r-md);
    padding: var(--s-2);
    background: var(--glass-strong);
    box-shadow: var(--glass-shadow);
}

.target-more-menu[hidden] { display: none; }

.target-menu-item {
    display: flex;
    width: 100%;
    align-items: center;
    justify-content: space-between;
    gap: var(--s-4);
    border: 0;
    border-radius: var(--r-sm);
    padding: var(--s-3);
    background: transparent;
    color: var(--text);
    cursor: pointer;
    text-align: left;
}

.target-menu-item:hover,
.target-menu-item.active { background: var(--accent-soft); color: var(--accent-ink); }
.target-menu-item small { color: var(--muted); font-size: 11px; }

.target-tab {
    flex: 1 1 0;
    min-width: 0;
    min-height: 36px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--s-2);
    border: 1px solid var(--line);
    border-radius: var(--r-pill);
    background: var(--glass);
    color: var(--muted);
    padding: var(--s-2) var(--s-3);
    cursor: pointer;
    white-space: nowrap;
}
.target-tab:hover { border-color: var(--line-strong); background: var(--glass-strong); }

.target-tab.active {
    border-color: var(--accent);
    background: var(--accent-soft);
    color: var(--accent-ink);
    box-shadow: var(--glass-inset);
}

.tab-icon {
    flex: 0 0 auto;
    width: 16px;
    height: 16px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: inherit;
}
.tab-icon svg { width: 16px; height: 16px; }

.tab-text { display: inline-flex; align-items: baseline; gap: 4px; line-height: 1; }
.tab-name { font-size: 13px; font-weight: 500; }
.tab-sub {
    font-size: 11px;
    font-weight: 400;
    opacity: 0.7;
    letter-spacing: 0.02em;
}
.target-tab.active .tab-sub { opacity: 0.9; }

/* ---------- Rows ---------- */
.action-row,
.feedback-row,
.result-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s-4);
}

.action-row {
    margin-top: var(--s-6);
    padding-top: var(--s-5);
    border-top: 1px solid var(--line);
}

.selected-target { margin: 0; color: var(--muted); font-size: 13px; font-weight: 400; }
.selected-target strong { color: var(--text); font-weight: 600; }

.primary-button,
.secondary-button,
.feedback-button {
    min-height: 44px;
    border-radius: var(--r-sm);
    cursor: pointer;
    padding: var(--s-2) var(--s-5);
    font-size: 14px;
    font-weight: 500;
}

.primary-button {
    border: 1px solid rgba(255, 255, 255, 0.45);
    background: var(--accent-grad);
    color: #ffffff;
    min-width: 132px;
    box-shadow: 0 8px 22px -8px rgba(13, 148, 136, 0.55), var(--glass-inset);
}
.primary-button:hover { filter: brightness(1.05); }
.primary-button:disabled { cursor: wait; opacity: 0.7; }

.secondary-button,
.feedback-button {
    border: 1px solid var(--line);
    background: var(--glass);
    color: var(--text);
}
.secondary-button:hover,
.feedback-button:hover { border-color: var(--line-strong); background: var(--glass-strong); }

/* ---------- Result panel ---------- */
.result-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s-4);
    min-height: 44px;
    padding-bottom: var(--s-3);
    border-bottom: 1px solid var(--line);
}

.result-header__right {
    display: flex;
    align-items: center;
    gap: var(--s-4);
}

.result-target {
    display: inline-flex;
    align-items: baseline;
    gap: 6px;
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    background: var(--glass-strong);
    padding: 7px var(--s-4);
    white-space: nowrap;
}
.result-target strong { font-size: 14px; font-weight: 600; }

.copy-button { min-width: 96px; font-size: 13px; }

.muted-label {
    display: block;
    color: var(--muted);
    font-size: 12px;
    font-weight: 500;
}

.output-stack {
    display: flex;
    flex: 1 1 0;
    min-height: 0;
    flex-direction: column;
    margin-top: var(--s-3);
}
#finalPrompt { flex: 1 1 auto; min-height: 0; overflow: auto; font-size: 13px; line-height: 1.6; }

.copy-message {
    min-height: 20px;
    margin: var(--s-3) 0 0;
    color: var(--muted);
    font-size: 12px;
}
.copy-message.success { color: var(--accent-ink); }
.copy-message.warning { color: var(--warning); }

.feedback-row { justify-content: flex-start; margin-top: var(--s-3); }
.feedback-button { min-width: 76px; color: var(--muted); }

/* ---------- Debug ---------- */
.debug-panel {
    flex: 0 0 auto;
    max-height: 240px;
    margin-top: var(--s-4);
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    background: var(--glass);
    padding: var(--s-4);
    overflow: auto;
}
.debug-panel summary { cursor: pointer; font-weight: 600; font-size: 13px; }

.debug-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--s-4);
    margin-top: var(--s-4);
}
.debug-block { margin-top: var(--s-4); }

.debug-panel code,
.debug-panel pre {
    display: block;
    width: 100%;
    margin: var(--s-2) 0 0;
    border: 1px solid var(--line);
    border-radius: var(--r-sm);
    background: var(--field-fill);
    color: var(--text);
    font-family: var(--font-mono);
    font-size: 12px;
}
.debug-panel code { padding: var(--s-2) var(--s-3); }
.debug-panel pre {
    max-height: 180px;
    overflow: auto;
    padding: var(--s-3);
    white-space: pre-wrap;
}

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
    body { overflow: auto; }
    .app-shell { height: auto; min-height: 100vh; padding: var(--s-4); }
    .top-bar, .model-bar, .workbench { display: flex; flex-direction: column; }
    .input-panel, .result-panel { min-height: 520px; }
}

@media (max-width: 560px) {
    .app-shell { padding: var(--s-3); }
    .action-row, .feedback-row, .result-header, .debug-grid {
        align-items: stretch;
        flex-direction: column;
    }
    .primary-button, .secondary-button, .feedback-button { width: 100%; }
    .result-target { text-align: left; }
}

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

/* Fallback: if backdrop-filter is unsupported, beef up opacity so panels stay legible */
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
    .top-bar, .model-bar, .input-panel, .result-panel,
    .target-tab, .secondary-button, .feedback-button,
    .target-more-menu, .result-target, .copy-button, .debug-panel, .primary-button {
        background: rgba(255, 255, 255, 0.92);
    }
    [data-theme="dark"] .top-bar, [data-theme="dark"] .model-bar,
    [data-theme="dark"] .input-panel, [data-theme="dark"] .result-panel,
    [data-theme="dark"] .target-tab, [data-theme="dark"] .secondary-button,
    [data-theme="dark"] .feedback-button, [data-theme="dark"] .target-more-menu,
    [data-theme="dark"] .result-target,
    [data-theme="dark"] .copy-button, [data-theme="dark"] .debug-panel,
    [data-theme="dark"] .primary-button {
        background: rgba(24, 27, 38, 0.96);
    }
}
