/* ─── CSS Variables ─── */
:root {
    --bg-primary: #0b0b14;
    --bg-secondary: #111120;
    --bg-tertiary: #1a1a2e;
    --bg-surface: #16162a;
    --bg-glass: rgba(255, 255, 255, 0.03);
    --border: rgba(255, 255, 255, 0.06);
    --border-hover: rgba(108, 99, 255, 0.3);
    --text-primary: #e8e8f4;
    --text-secondary: #8888a0;
    --text-muted: #555570;
    --accent: #6c63ff;
    --accent-light: #8b83ff;
    --accent-glow: rgba(108, 99, 255, 0.2);
    --accent-2: #ff6b9d;
    --accent-3: #00d4aa;
    --accent-4: #f7b731;
    --gradient-1: linear-gradient(135deg, #6c63ff, #a855f7);
    --radius: 10px;
    --radius-sm: 6px;
    --radius-lg: 14px;
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
    --shadow-table: 0 8px 32px rgba(0, 0, 0, 0.5);
    --transition: 0.2s ease;
    --font: 'Inter', -apple-system, sans-serif;
    --font-mono: 'JetBrains Mono', 'SF Mono', monospace;

    /* Table colors */
    --table-blue: #3b82f6;
    --table-purple: #8b5cf6;
    --table-pink: #ec4899;
    --table-green: #10b981;
    --table-orange: #f59e0b;
    --table-cyan: #06b6d4;
    --table-red: #ef4444;
    --table-indigo: #6366f1;
}

/* ─── Reset ─── */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
}

body {
    font-family: var(--font);
    background: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
}

/* ─── App Layout ─── */
.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* ─── Toolbar ─── */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    z-index: 100;
    gap: 12px;
    flex-wrap: wrap;
}

.toolbar-left,
.toolbar-center,
.toolbar-right {
    display: flex;
    align-items: center;
    gap: 6px;
}

.logo {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 1rem;
    font-weight: 700;
    background: var(--gradient-1);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.toolbar-divider {
    width: 1px;
    height: 24px;
    background: var(--border);
    margin: 0 4px;
}

.tool-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 6px 10px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-family: var(--font);
    font-size: 0.75rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
}

.tool-btn:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border-color: var(--border-hover);
}

.tool-btn-primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.tool-btn-primary:hover {
    background: var(--accent-light);
    border-color: var(--accent-light);
}

.toolbar select {
    padding: 6px 28px 6px 8px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 0.75rem;
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg width='8' height='5' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M0 0l4 5 4-5z' fill='%238888a0'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
}

.toolbar select:focus {
    outline: none;
    border-color: var(--accent);
}

/* ─── Canvas Area ─── */
.canvas-area {
    flex: 1;
    position: relative;
    overflow: hidden;
    background:
        radial-gradient(circle at 20% 30%, rgba(108, 99, 255, 0.04) 0%, transparent 40%),
        radial-gradient(circle at 80% 70%, rgba(255, 107, 157, 0.03) 0%, transparent 40%),
        var(--bg-primary);
    cursor: grab;
}

.canvas-area:active {
    cursor: grabbing;
}

canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    pointer-events: none;
    /* Lines drawn underneath tables */
}

.tables-layer {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    transform-origin: 0 0;
}

/* ─── Table Card ─── */
.table-card {
    position: absolute;
    min-width: 200px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-table);
    cursor: move;
    transition: box-shadow 0.15s ease;
    user-select: none;
}

.table-card:hover {
    box-shadow: var(--shadow-table), 0 0 0 1px var(--border-hover);
}

.table-card.dragging {
    z-index: 100;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6), 0 0 0 2px var(--accent);
}

.table-card.selected {
    box-shadow: var(--shadow-table), 0 0 0 2px var(--accent);
}

.table-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border-radius: var(--radius) var(--radius) 0 0;
    cursor: move;
}

.table-header-left {
    display: flex;
    align-items: center;
    gap: 8px;
}

.table-color-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

.table-name {
    font-size: 0.82rem;
    font-weight: 600;
    color: #fff;
}

.table-actions {
    display: flex;
    gap: 4px;
}

.table-action-btn {
    width: 22px;
    height: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
    color: rgba(255, 255, 255, 0.6);
    cursor: pointer;
    font-size: 0.65rem;
    transition: all var(--transition);
}

.table-action-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
}

.table-columns {
    padding: 0;
    list-style: none;
}

.table-column {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    border-top: 1px solid rgba(255, 255, 255, 0.04);
    font-size: 0.75rem;
    transition: background var(--transition);
}

.table-column:hover {
    background: rgba(255, 255, 255, 0.03);
}

.col-icon {
    font-size: 0.65rem;
    width: 14px;
    text-align: center;
    flex-shrink: 0;
}

.col-name {
    font-family: var(--font-mono);
    font-size: 0.72rem;
    color: var(--text-primary);
    flex: 1;
}

.col-type {
    font-family: var(--font-mono);
    font-size: 0.65rem;
    color: var(--text-muted);
    text-transform: uppercase;
}

.col-constraints {
    display: flex;
    gap: 3px;
}

.constraint-badge {
    padding: 1px 4px;
    border-radius: 3px;
    font-size: 0.5rem;
    font-weight: 600;
    text-transform: uppercase;
}

.badge-pk {
    background: rgba(108, 99, 255, 0.2);
    color: var(--accent);
}

.badge-fk {
    background: rgba(255, 107, 157, 0.2);
    color: var(--accent-2);
}

.badge-nn {
    background: rgba(0, 212, 170, 0.15);
    color: var(--accent-3);
}

.badge-uq {
    background: rgba(247, 183, 49, 0.2);
    color: var(--accent-4);
}

/* ─── Zoom Indicator ─── */
.zoom-indicator {
    position: absolute;
    bottom: 16px;
    right: 16px;
    padding: 6px 12px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 0.72rem;
    font-family: var(--font-mono);
    color: var(--text-secondary);
    z-index: 50;
}

/* ─── Side Panel ─── */
.side-panel {
    position: absolute;
    top: 0;
    right: 0;
    width: 340px;
    height: 100%;
    background: var(--bg-secondary);
    border-left: 1px solid var(--border);
    z-index: 200;
    display: flex;
    flex-direction: column;
    box-shadow: -4px 0 20px rgba(0, 0, 0, 0.3);
    transition: transform 0.25s ease;
}

.side-panel.hidden {
    display: none;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
}

.panel-header h2 {
    font-size: 0.9rem;
    font-weight: 600;
}

.panel-close {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: var(--bg-glass);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.9rem;
    transition: all var(--transition);
}

.panel-close:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.panel-body {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

.form-group {
    margin-bottom: 14px;
}

.form-group label {
    display: block;
    font-size: 0.72rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 8px 10px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: var(--font-mono);
    font-size: 0.8rem;
    transition: border-color var(--transition);
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--accent);
}

.columns-list {
    list-style: none;
}

.column-row {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.03);
}

.column-row input {
    flex: 1;
    min-width: 0;
}

.column-row select {
    width: 90px;
    flex-shrink: 0;
}

.column-row .col-checks {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
}

.col-checks label {
    display: flex;
    align-items: center;
    gap: 3px;
    font-size: 0.6rem;
    color: var(--text-muted);
    cursor: pointer;
    text-transform: uppercase;
}

.col-checks input[type="checkbox"] {
    width: 14px;
    height: 14px;
    accent-color: var(--accent);
}

.column-remove-btn {
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: rgba(255, 0, 0, 0.1);
    border-radius: 4px;
    color: #ff4757;
    cursor: pointer;
    font-size: 0.65rem;
    flex-shrink: 0;
    transition: all var(--transition);
}

.column-remove-btn:hover {
    background: rgba(255, 0, 0, 0.2);
}

.btn {
    padding: 8px 14px;
    border: none;
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 0.8rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
}

.btn-primary {
    background: var(--gradient-1);
    color: #fff;
}

.btn-primary:hover {
    opacity: 0.9;
}

.btn-ghost {
    background: var(--bg-glass);
    color: var(--text-secondary);
    border: 1px solid var(--border);
}

.btn-ghost:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.btn-danger {
    background: rgba(255, 0, 0, 0.1);
    color: #ff4757;
}

.btn-danger:hover {
    background: rgba(255, 0, 0, 0.2);
}

.btn-sm {
    padding: 5px 10px;
    font-size: 0.72rem;
}

.panel-footer {
    display: flex;
    gap: 8px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
    margin-top: 12px;
}

/* ─── Color Swatches ─── */
.color-swatches {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

.color-swatch {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 2px solid transparent;
    cursor: pointer;
    transition: all var(--transition);
}

.color-swatch:hover {
    transform: scale(1.15);
}

.color-swatch.selected {
    border-color: #fff;
    box-shadow: 0 0 8px var(--accent-glow);
}

/* ─── Modal ─── */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 500;
}

.modal-overlay.hidden {
    display: none;
}

.modal {
    width: 90%;
    max-width: 600px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 24px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.modal h2 {
    font-size: 1rem;
    margin-bottom: 8px;
}

.modal p {
    font-size: 0.82rem;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

.modal textarea {
    width: 100%;
    padding: 12px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: var(--font-mono);
    font-size: 0.8rem;
    resize: vertical;
}

.modal textarea:focus {
    outline: none;
    border-color: var(--accent);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 16px;
}

/* ─── Grid pattern (drawn on canvas) ─── */

/* ─── Responsive ─── */
@media (max-width: 768px) {
    .toolbar {
        padding: 6px 10px;
    }

    .toolbar-left .logo span {
        display: none;
    }

    .tool-btn span {
        display: none;
    }

    .side-panel {
        width: 100%;
    }
}