:root {
    --surface: #f5f7fb;
    --border: #d6d9e6;
    --primary: #275efe;
    --primary-dark: #1740b6;
    --text: #1d2333;
    --muted: #5c6370;
    --background: #ffffff;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: "Inter", "Segoe UI", Tahoma, sans-serif;
    color: var(--text);
    background: var(--surface);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.5rem;
    background: var(--background);
    border-bottom: 1px solid var(--border);
    box-shadow: 0 1px 3px rgba(29, 35, 51, 0.08);
    position: sticky;
    top: 0;
    z-index: 10;
}

.brand {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.brand-link {
    display: inline-flex;
    align-items: center;
}

.brand-logo {
    height: 36px;
}

.brand-copy {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

.brand-copy h1 {
    margin: 0;
    font-size: 1.4rem;
}

.brand-copy p {
    margin: 0;
    font-size: 0.85rem;
    color: var(--muted);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.header-actions .ghost {
    text-decoration: none;
    padding: 0.5rem 0.95rem;
    border-radius: 6px;
    font-size: 0.95rem;
}

button {
    cursor: pointer;
    border: none;
    border-radius: 6px;
    padding: 0.5rem 1rem;
    font-size: 0.95rem;
    font-weight: 600;
    transition: background 0.2s ease, color 0.2s ease, transform 0.1s ease;
}

button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

button.primary {
    background: var(--primary);
    color: #fff;
}

button.primary:hover {
    background: var(--primary-dark);
}

button.secondary {
    background: #e4e8f7;
    color: var(--primary);
}

button.secondary:hover {
    background: #d0d7f3;
}

button.danger {
    background: #f7d4d6;
    color: #b42318;
}

button.danger:hover {
    background: #f4bfc2;
}

.app-main {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 0;
    flex: 1;
    overflow: hidden;
}

.toolbar {
    grid-column: 1 / span 2;
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 1rem;
    padding: 1rem 1.5rem;
    background: var(--background);
    border-bottom: 1px solid var(--border);
}

.control {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    min-width: 180px;
}

.control label {
    font-size: 0.85rem;
    color: var(--muted);
}

select,
input[type="number"],
input[type="text"],
textarea {
    padding: 0.45rem 0.6rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.95rem;
    font-family: inherit;
    background: #fff;
}

textarea {
    resize: vertical;
}

.grid-size .grid-inputs {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.grid-wrapper {
    padding: 1rem 1.5rem;
    overflow: auto;
    background: var(--surface);
}

.grid-container {
    min-width: 100%;
    overflow: auto;
    background: var(--background);
    border: 1px solid var(--border);
    border-radius: 8px;
    position: relative;
}

.blank-template-callout {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    background: linear-gradient(135deg, rgba(15, 23, 42, 0.65), rgba(15, 23, 42, 0.35));
    backdrop-filter: blur(3px);
    z-index: 10;
}

.blank-template-callout[hidden] {
    display: none;
}

.blank-template-card {
    width: min(420px, 100%);
    background: #ffffff;
    border-radius: 16px;
    box-shadow: 0 24px 48px rgba(15, 23, 42, 0.18);
    padding: 2rem;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.blank-template-card h2 {
    font-size: 1.5rem;
    margin: 0;
    color: var(--text);
}

.blank-template-card p {
    margin: 0;
    color: var(--muted);
    line-height: 1.5;
}

.blank-template-controls {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.blank-template-controls label {
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
}

.blank-template-actions {
    display: flex;
    gap: 0.75rem;
}

.blank-template-actions select {
    flex: 1;
    padding: 0.65rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 0.95rem;
    color: var(--text);
    background: #f8f9ff;
}

#duplicateTemplateButton {
    white-space: nowrap;
    min-width: 150px;
}

#dismissBlankTemplateCallout {
    align-self: flex-start;
}

#gridTable {
    border-collapse: separate;
    border-spacing: 0;
    width: max-content;
}

#gridTable th,
#gridTable td {
    min-width: 100px;
    min-height: 10px;
    position: relative;
}

#gridTable th {
    border: 1px solid var(--border);
    background: #f0f3ff;
    font-weight: 600;
    color: var(--muted);
    text-align: center;
    user-select: none;
}

#gridTable td {
    border: none;
}

#gridTable tbody th {
    width: 42px;
}

#gridTable th.corner-cell {
    background: #f0f3ff;
    border-right: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    width: 42px;
}

#gridTable td.cell {
    background: #fff;
    vertical-align: top;
    padding: 0.3rem;
    font-size: 0.95rem;
    color: var(--text);
    overflow: hidden;
}

#gridTable td.cell .cell-content {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: flex-start;
    align-items: flex-start;
    overflow: hidden;
    word-break: break-word;
}

#gridTable td.cell.selected {
    outline: 2px solid var(--primary);
    z-index: 1;
}

#gridTable td.cell.range-selected:not(.selected) {
    background: rgba(39, 94, 254, 0.08);
}

#gridTable td.cell img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.cell-editor {
    background: var(--background);
    border-left: 1px solid var(--border);
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    width: 360px;
    min-width: 320px;
    max-width: 560px;
    resize: horizontal;
    overflow-y: auto;
    overflow-x: hidden;
}

.editor-block {
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
    padding: 1rem 1.25rem;
    background: #ffffff;
    border: 1px solid rgba(18, 34, 72, 0.08);
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(29, 35, 51, 0.05);
}

.editor-block__header h2,
.editor-block__header h3 {
    margin: 0;
    font-size: 1.05rem;
}

.editor-block__header p {
    margin: 0;
    font-size: 0.8rem;
    color: var(--muted);
}

.editor-block__body {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.selection-display {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.cell-info {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
}

.cell-info .label {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
}

.editor-group {
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
}

.group-label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--muted);
}

.radio-group {
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
}

.toggle-buttons {
    display: flex;
    gap: 0.5rem;
}

.toggle-buttons button {
    min-width: 40px;
    height: 36px;
    padding: 0 0.5rem;
    background: #e8ecfb;
    color: var(--text);
    border-radius: 4px;
    font-weight: 700;
}

.toggle-buttons button.active {
    background: var(--primary);
    color: #fff;
}

.colors {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.6rem 0.75rem;
    align-items: center;
}

.colors input[type="color"] {
    width: 100%;
    height: 38px;
    padding: 0;
}

.conditional-row {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    flex-wrap: wrap;
}

.conditional-row select,
.conditional-row input[type="text"] {
    flex: 1;
}

.conditional-actions {
    display: flex;
    justify-content: flex-end;
}

.border-controls {
    gap: 0.35rem;
}

.border-actions {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.border-actions .ghost {
    margin-left: auto;
}

.border-inputs {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.border-inputs input[type="color"] {
    flex: 1;
    height: 38px;
    padding: 0;
}

.border-inputs input[type="number"] {
    width: 80px;
}

.border-range {
    gap: 0.6rem;
    padding: 0.85rem;
    border: 1px solid rgba(18, 34, 72, 0.08);
    border-radius: 10px;
    background: rgba(39, 94, 254, 0.04);
}

.border-range__fields {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 0.5rem;
    align-items: center;
}

.border-range__fields label {
    font-size: 0.8rem;
    color: var(--muted);
}

.border-range__fields input[type="text"] {
    text-transform: uppercase;
}

.border-range__mode {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}


.border-range__actions,
.row-ops__actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.5rem;
    align-items: center;
}

.border-range__actions .ghost {
    justify-self: end;
}

.border-hint,
.editor-hint {
    font-size: 0.8rem;
    color: var(--muted);
}

.alignment-options {
    display: flex;
    gap: 0.5rem;
}

.image-preview {
    width: 100%;
    min-height: 80px;
    border: 1px dashed var(--border);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem;
    background: #fafbff;
}

.image-preview img {
    max-width: 100%;
    max-height: 120px;
    object-fit: contain;
}

.merge-controls {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.merge-buttons {
    display: flex;
    gap: 0.5rem;
}

.ghost {
    background: transparent;
    color: var(--muted);
    border: 1px solid rgba(18, 34, 72, 0.2);
}

.ghost:hover {
    background: rgba(18, 34, 72, 0.05);
}

.merge-span-inputs {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.4rem 0.5rem;
    align-items: center;
}

.merge-span-inputs label {
    font-size: 0.85rem;
    color: var(--muted);
}

.merge-span-inputs input {
    width: 100%;
    padding: 0.35rem 0.5rem;
}

.merge-hint {
    font-size: 0.8rem;
    color: var(--muted);
}

#status {
    min-height: 24px;
    color: var(--muted);
}

/* Resizers */
.column-resizer,
.row-resizer {
    position: absolute;
    background: transparent;
    z-index: 2;
}

.column-resizer {
    top: 0;
    right: -3px;
    width: 6px;
    height: 100%;
    cursor: col-resize;
}

.row-resizer {
    bottom: -3px;
    left: 0;
    width: 100%;
    height: 6px;
    cursor: row-resize;
}

.column-resizer::after,
.row-resizer::after {
    content: "";
    position: absolute;
    inset: 0;
}

@media (max-width: 1100px) {
    .app-main {
        grid-template-columns: 1fr;
    }

    .toolbar {
        grid-column: 1;
    }

    .cell-editor {
        border-left: none;
        border-top: 1px solid var(--border);
        grid-row: 3;
        width: 100%;
        min-width: 0;
        max-width: none;
        resize: none;
        overflow-x: auto;
    }
}

.column-badge {
    display: inline-flex;
    align-items: center;
    padding: 0.15rem 0.4rem;
    border-radius: 999px;
    background: rgba(39, 94, 254, 0.12);
    color: var(--primary-dark);
    font-size: inherit;
    font-weight: 600;
}

.info {
    color: var(--muted);
}

.success {
    color: #2c9a4b;
}

.warning {
    color: #d69600;
}

.error {
    color: #d14343;
}