:root {
    --bg: #f5f7f9;
    --surface: #ffffff;
    --text: #17202a;
    --muted: #687381;
    --line: #d9e0e7;
    --accent: #1864ab;
    --accent-dark: #124f86;
    --danger: #b42318;
    --success: #067647;
}

* { box-sizing: border-box; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, Helvetica, sans-serif;
    line-height: 1.45;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    padding: 14px 28px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; color: var(--text); font-size: 20px; }
.topbar nav { display: flex; align-items: center; gap: 16px; }
.topbar form { margin: 0; }

.page, .public-shell {
    width: min(1180px, calc(100% - 32px));
    margin: 28px auto;
}
.public-shell { width: min(820px, calc(100% - 32px)); }

.panel {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 22px;
    margin-bottom: 20px;
}
.narrow { max-width: 560px; margin-left: auto; margin-right: auto; }
.heading-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 20px;
    margin-bottom: 18px;
}
.heading-row > div { min-width: 0; }
.editor-meta {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    color: var(--muted);
    font-size: 14px;
}
.editor-meta a {
    display: block;
    max-width: min(620px, 100%);
    min-width: 0;
    overflow-wrap: anywhere;
    word-break: break-all;
}
.status-dot {
    display: inline-flex;
    align-items: center;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 2px 9px;
    background: #f8fafc;
    color: var(--muted);
    font-weight: 700;
    text-transform: capitalize;
}
.status-published { color: var(--success); background: #ecfdf3; border-color: #abefc6; }
.status-archived { color: #7a271a; background: #fff4ed; border-color: #fedf89; }
.tabs {
    display: flex;
    gap: 8px;
    border-bottom: 1px solid var(--line);
    margin-bottom: 0;
}
.editor-tabs { margin-bottom: 16px; }
.tab-button {
    border: 0;
    border-bottom: 3px solid transparent;
    border-radius: 0;
    background: transparent;
    color: var(--muted);
    min-height: 44px;
}
.tab-button.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
    font-weight: 700;
}
.tab-panel { display: none; }
.tab-panel.active { display: block; }
.editor-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
}
.editor-actions form { margin: 0; }
h1, h2 {
    margin: 0 0 12px;
    line-height: 1.2;
    overflow-wrap: anywhere;
    word-break: break-word;
}
h1 { font-size: 32px; }
h2 { font-size: 26px; }
p { color: var(--muted); margin-top: 0; }
.section-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 20px;
    margin-bottom: 16px;
}
.section-header h2 { margin-bottom: 4px; }
.section-header p { margin-bottom: 0; }

form.stack, .stack { display: grid; gap: 14px; }
.grid-form {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
}
.span { grid-column: 1 / -1; }
label { display: grid; gap: 6px; font-weight: 600; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; }
.check input[type="checkbox"],
.check input[type="radio"] {
    width: auto;
    min-width: 16px;
    padding: 0;
}
.toggle-check {
    justify-content: flex-start;
    min-height: 44px;
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 10px 12px;
    background: #f8fafc;
}
input, select, textarea {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: #fff;
    color: var(--text);
    font: inherit;
}
textarea { min-height: 92px; resize: vertical; }
button, .button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: #fff;
    color: var(--text);
    padding: 9px 13px;
    font: inherit;
    cursor: pointer;
    min-height: 40px;
}
.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}
.primary:hover { background: var(--accent-dark); }
.danger {
    border-color: #f3b8b2;
    color: var(--danger);
    background: #fff7f6;
}
.inline-create {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: min(460px, 100%);
}
.field-type-picker {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(102px, 1fr));
    gap: 10px;
    max-height: 82px;
    overflow: hidden;
}
.field-palette-panel {
    position: sticky;
    top: 0;
    z-index: 20;
}
.field-palette-panel.expanded .field-type-picker {
    max-height: none;
}
.field-type-button {
    align-items: center;
    min-height: 82px;
    font-weight: 700;
    flex-direction: column;
    gap: 8px;
    text-align: center;
    padding: 14px 10px;
    box-shadow: 0 1px 2px rgba(15, 23, 42, .04);
}
.field-type-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    min-height: 22px;
}
.field-type-icon img {
    display: block;
    width: 20px;
    height: 20px;
    object-fit: contain;
    opacity: .72;
}
.field-type-button span:last-child {
    font-size: 13px;
}

.table-wrap { overflow-x: auto; }
table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
    border: 1px solid var(--line);
}
th, td {
    text-align: left;
    border-bottom: 1px solid var(--line);
    padding: 11px 12px;
    vertical-align: top;
}
th { background: #eef2f6; }
.table-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}
.badge {
    display: inline-block;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 2px 8px;
    color: var(--muted);
    font-size: 13px;
}
.alert {
    border-radius: 6px;
    padding: 11px 13px;
    margin-bottom: 16px;
    border: 1px solid var(--line);
}
.alert.success { background: #ecfdf3; border-color: #abefc6; color: var(--success); }
.alert.error { background: #fef3f2; border-color: #fecdca; color: var(--danger); }
.requirements { display: grid; gap: 8px; margin-bottom: 18px; }
.ok { color: var(--success); font-weight: 700; }
.bad { color: var(--danger); font-weight: 700; }
.item {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 14px;
    margin-bottom: 12px;
}
.item, .item .actions, .compact {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
}
.field-item { flex-wrap: wrap; }
.field-card-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 14px;
    width: 100%;
}
.field-summary {
    flex: 1 1 auto;
    min-width: 0;
    display: grid;
    gap: 6px;
}
.field-title-line {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}
.field-meta-line {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    color: var(--muted);
    font-size: 13px;
}
.field-meta-line span {
    border-right: 1px solid var(--line);
    padding-right: 8px;
}
.field-meta-line span:last-child {
    border-right: 0;
    padding-right: 0;
}
.field-settings {
    flex-basis: 100%;
    width: 100%;
    border-top: 1px solid var(--line);
    padding-top: 12px;
}
.field-option-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px 14px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px;
    background: #f8fafc;
}
.field-option-grid strong {
    grid-column: 1 / -1;
}
.form-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}
.item form { margin: 0; }
.muted { color: var(--muted); display: block; }
.field-list {
    display: grid;
    gap: 12px;
}
.field-list .field-item {
    margin-bottom: 0;
}
.field-item.dragging {
    opacity: .62;
}
.field-item.drag-over {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(24, 100, 171, .12);
}
.drag-handle {
    align-self: stretch;
    min-width: 52px;
    min-height: 44px;
    padding: 8px 10px;
    color: var(--muted);
    background: #f8fafc;
    cursor: grab;
    font-size: 13px;
    font-weight: 700;
}
.drag-handle img {
    display: block;
    width: 18px;
    height: 18px;
    opacity: .66;
    pointer-events: none;
}
.drag-handle:active {
    cursor: grabbing;
}
.move-field:disabled {
    cursor: not-allowed;
    opacity: .45;
}
.option-editor {
    display: grid;
    gap: 10px;
    margin-top: 12px;
}
.option-editor-wrap,
.condition-editor {
    flex-basis: 100%;
    width: 100%;
    border-top: 1px solid var(--line);
    padding-top: 12px;
}
.option-editor-wrap summary,
.condition-editor summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    cursor: pointer;
    font-weight: 700;
}
.option-editor-wrap summary small,
.condition-editor summary small {
    color: var(--muted);
    font-weight: 500;
}
.option-toolbar {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 12px;
}
.option-list {
    display: grid;
    gap: 10px;
}
.option-row {
    display: grid;
    grid-template-columns: minmax(180px, 1fr) minmax(180px, 1fr) auto auto;
    align-items: end;
    gap: 10px;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #f8fafc;
}
.option-row .default-choice { min-height: 40px; }
.logic-row {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 10px;
    margin-bottom: 14px;
}
.condition-editor {
    margin-top: 12px;
    display: grid;
    gap: 10px;
}
.condition-editor[open] { gap: 12px; }
.condition-row {
    display: grid;
    grid-template-columns: minmax(170px, 1.1fr) minmax(140px, .85fr) minmax(130px, .8fr) minmax(150px, .85fr) auto auto;
    align-items: end;
    gap: 10px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #f8fafc;
}
.condition-pill {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #f8fafc;
}
.condition-pill form { margin: 0; }
.delete-form { margin-top: 24px; }
.field { margin-bottom: 18px; }
.field[hidden] { display: none; }
.field small { display: block; color: var(--muted); margin-top: 5px; }
.public-form {
    background: var(--form-bg, var(--surface));
    color: var(--form-text-color, var(--text));
}
.public-form h1 { color: var(--form-title-color, var(--text)); }
.public-form > p { color: var(--form-description-color, var(--muted)); }
.public-form label,
.public-form .field > label > span { color: var(--form-label-color, var(--text)); }
.public-form.theme-clean {
    --form-bg: #ffffff;
}
.public-form.theme-midnight {
    --form-bg: #101828;
    --form-title-color: #f9fafb;
    --form-description-color: #cbd5e1;
    --form-label-color: #e5e7eb;
    --form-text-color: #f8fafc;
}
.public-form.theme-garden {
    --form-bg: #f1f8f3;
    --form-title-color: #14532d;
    --form-description-color: #3f6212;
    --form-label-color: #1f3d2b;
    --form-text-color: #17202a;
}
.field-error { color: var(--danger); margin-top: 6px; }
.field-picture {
    display: flex;
    margin-bottom: 10px;
}
.field-picture-left { justify-content: flex-start; }
.field-picture-center { justify-content: center; }
.field-picture-right { justify-content: flex-end; }
.field-picture img {
    display: block;
    max-width: 100%;
    border-radius: 6px;
}
.choice-stack {
    display: grid;
    gap: 8px;
}
.subfield-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
}
.email-otp-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 10px;
    align-items: center;
}
.choice-stack .check,
.consent-check {
    justify-content: flex-start;
    min-height: 0;
}
.input-prefix {
    display: flex;
    align-items: stretch;
}
.input-prefix span {
    display: inline-flex;
    align-items: center;
    border: 1px solid var(--line);
    border-right: 0;
    border-radius: 6px 0 0 6px;
    background: #f8fafc;
    color: var(--muted);
    padding: 0 12px;
    font-weight: 700;
}
.input-prefix input {
    border-radius: 0 6px 6px 0;
}
.form-section {
    border-bottom: 1px solid var(--line);
    padding-bottom: 10px;
}
.form-section h2 {
    font-size: 21px;
    margin-bottom: 6px;
}
.html-field {
    color: var(--text);
}
.page-break {
    border: 0;
    border-top: 1px dashed var(--line);
    margin: 8px 0;
}
.signature-pad {
    width: 100%;
    height: 220px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: #fff;
    touch-action: none;
}

@media (max-width: 760px) {
    .topbar, .heading-row, .item, .inline-create, .section-header, .field-card-top { flex-direction: column; align-items: stretch; }
    .topbar nav { flex-wrap: wrap; }
    h1 { font-size: 25px; }
    h2 { font-size: 24px; }
    .editor-meta {
        display: grid;
        grid-template-columns: auto 1fr;
        align-items: center;
    }
    .editor-meta a { grid-column: 1 / -1; }
    .grid-form, .logic-row, .option-row, .condition-row, .field-option-grid, .subfield-grid, .email-otp-row { grid-template-columns: 1fr; }
    .field-type-picker { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .condition-pill { flex-direction: column; align-items: stretch; }
    .form-actions, .option-toolbar, .editor-actions, .item .actions { flex-direction: column; align-items: stretch; }
    .drag-handle {
        align-self: flex-start;
        width: 100%;
    }
}

@media (max-width: 420px) {
    .page, .public-shell { width: min(100% - 24px, 1180px); }
    .panel { padding: 18px; }
    h1 { font-size: 24px; }
    .field-type-button { padding: 10px; }
    .field-type-picker { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
