:root {
    --bg: #f4f3ef;
    --panel: #ffffff;
    --ink: #1b1a17;
    --muted: #6b6b66;
    --accent: #0b543d;
    --accent-2: #0f6b4e;
    --line: #d9d6cf;
    --danger: #b3412e;
    --success: #2c7a4b;
    --radius: 14px;
    --shadow: 0 12px 30px rgba(20, 18, 12, 0.12);
    --shadow-soft: 0 6px 16px rgba(20, 18, 12, 0.08);
    --font: "Tahoma", "Segoe UI", "Noto Sans", sans-serif;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: var(--font);
    color: var(--ink);
    background: radial-gradient(circle at top left, #fdfaf1, var(--bg));
    -webkit-text-size-adjust: 100%;
}

body.no-select {
    user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
}

a {
    color: inherit;
    text-decoration: none;
}

h1, h2, h3 {
    margin: 0 0 12px;
    font-weight: 700;
}

p {
    margin: 0 0 12px;
}

.muted {
    color: var(--muted);
    font-size: 0.95rem;
}

.container {
    max-width: 1100px;
    margin: 32px auto;
    padding: 0 20px 40px;
    display: grid;
    gap: 24px;
}

.card {
    background: var(--panel);
    padding: 24px;
    border-radius: var(--radius);
    box-shadow: var(--shadow-soft);
}

.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 18px 24px;
    background: var(--panel);
    box-shadow: var(--shadow-soft);
    position: sticky;
    top: 0;
    z-index: 5;
    gap: 16px;
}

.top-bar nav a {
    margin-left: 16px;
    font-weight: 600;
}

.table-toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: 12px;
    border: 1px solid var(--line);
    background: #f6f1e7;
    white-space: nowrap;
}

.table-toolbar .muted {
    font-weight: 600;
}

.table-color-input {
    position: absolute;
    width: 0;
    height: 0;
    opacity: 0;
    pointer-events: none;
}

.voice-bar {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    max-width: 520px;
}

.voice-bar input {
    width: 100%;
    max-width: 420px;
    background: #fff;
}

.mic-btn {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    display: grid;
    place-items: center;
    background: #f1efe9;
    border: 1px solid var(--line);
    padding: 0;
}

.copy-btn {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    display: grid;
    place-items: center;
    background: #f1efe9;
    border: 1px solid var(--line);
    padding: 0;
}

.copy-btn svg {
    width: 20px;
    height: 20px;
    fill: var(--ink);
    display: block;
}

.copy-btn.copied {
    background: #e7f4eb;
    border-color: #b7dbc6;
}

.export-copy-row {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
    margin-top: 10px;
}

.export-copy-row select {
    flex: 1;
}

.export-copy-row button {
    white-space: nowrap;
}

.mic-btn svg {
    width: 20px;
    height: 20px;
    fill: var(--ink);
    display: block;
}

.mic-btn.active {
    background: var(--accent);
}

.mic-btn.active svg {
    fill: #fff;
}

.voice-fab {
    position: fixed;
    right: 24px;
    bottom: 24px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    background: var(--accent);
    color: #fff;
    box-shadow: var(--shadow);
    z-index: 50;
    opacity: 0;
    transform: translateY(10px);
    pointer-events: none;
    transition: opacity 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.voice-fab svg {
    width: 24px;
    height: 24px;
    fill: currentColor;
    display: block;
}

.voice-fab.is-visible {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

.voice-fab.is-listening {
    background: var(--accent-2);
    box-shadow: 0 14px 30px rgba(11, 84, 61, 0.25);
}

.voice-fab:disabled {
    background: #c9c5bc;
    cursor: not-allowed;
    opacity: 0.85;
}

@media (max-width: 720px) {
    .voice-fab {
        right: 16px;
        bottom: 16px;
        width: 52px;
        height: 52px;
    }
}

.stack {
    display: grid;
    gap: 12px;
}

label span {
    display: block;
    margin-bottom: 6px;
    font-size: 0.9rem;
}

input, select, textarea {
    width: 100%;
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid var(--line);
    font-family: var(--font);
    font-size: 1rem;
    background: #fff;
}

button {
    border: none;
    border-radius: 10px;
    padding: 10px 14px;
    font-weight: 700;
    cursor: pointer;
    font-family: var(--font);
    background: var(--line);
}

button.primary {
    background: var(--accent);
    color: #fff;
}

button.ghost {
    background: transparent;
    border: 1px solid var(--line);
}

button.danger {
    background: var(--danger);
    color: #fff;
}

.alert {
    padding: 10px 14px;
    border-radius: 10px;
    margin-bottom: 12px;
}

.alert.error {
    background: #f7e7e5;
    color: #8b2c1d;
}

.alert.success {
    background: #e7f4eb;
    color: #2c6a47;
}

.list {
    display: grid;
    gap: 12px;
}

.list-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    border-radius: 12px;
    border: 1px solid var(--line);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
    background: #fff;
}

.list-item:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-soft);
}

.pill {
    display: inline-flex;
    padding: 6px 10px;
    border-radius: 999px;
    background: #ece7dc;
    font-size: 0.85rem;
}

.pill-admin {
    background: #f3e2bf;
}

.pill-user {
    background: #e2ecf3;
}

.table {
    display: grid;
    gap: 8px;
}

.table-row {
    display: grid;
    grid-template-columns: 1.2fr 1fr 1.2fr 1fr;
    gap: 12px;
    align-items: center;
    padding: 10px 12px;
    border-radius: 10px;
    background: #fff;
    border: 1px solid var(--line);
}

.table-head {
    font-weight: 700;
    background: #f1efe9;
}

.inline-form {
    display: inline-flex;
    gap: 8px;
    align-items: center;
}

.auth-page {
    display: grid;
    place-items: center;
    min-height: 100vh;
    padding: 24px;
}

.auth-card {
    width: min(420px, 100%);
    background: var(--panel);
    padding: 32px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    text-align: left;
}

.hint {
    font-size: 0.85rem;
    color: var(--muted);
}

.designer-layout {
    display: grid;
    grid-template-columns: 260px 1fr 260px;
    min-height: calc(100vh - 70px);
}

.side-panel {
    background: #f6f1e7;
    padding: 20px;
    display: grid;
    gap: 18px;
}

.side-panel-left {
    border-right: 1px solid var(--line);
}

.side-panel-right {
    border-left: 1px solid var(--line);
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.side-toggle {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    display: grid;
    place-items: center;
    background: #f1efe9;
    border: 1px solid var(--line);
    padding: 0;
}

.side-toggle svg {
    width: 18px;
    height: 18px;
    fill: var(--ink);
    transition: transform 0.2s ease;
}

.side-toggle-mini {
    position: absolute;
    top: 16px;
    left: 12px;
    z-index: 3;
    display: none;
}

.side-toggle-right-mini {
    left: auto;
    right: 12px;
}

.side-toggle-right svg,
.side-toggle-right-mini svg {
    transform: rotate(180deg);
}

.sidebar-collapsed .designer-layout {
    grid-template-columns: 1fr 260px;
}

.sidebar-collapsed .side-panel-left {
    display: none;
}

.sidebar-collapsed .side-panel-left > * {
    display: none;
}

.sidebar-collapsed .side-toggle-left-mini {
    display: grid;
}

.sidebar-collapsed .side-toggle-left svg,
.sidebar-collapsed .side-toggle-left-mini svg {
    transform: rotate(180deg);
}

.sidebar-right-collapsed .designer-layout {
    grid-template-columns: 260px 1fr;
}

.sidebar-right-collapsed .side-panel-right {
    display: none;
}

.sidebar-right-collapsed .side-panel-right > * {
    display: none;
}

.sidebar-right-collapsed .side-toggle-right-mini {
    display: grid;
}

.sidebar-right-collapsed .side-toggle-right svg,
.sidebar-right-collapsed .side-toggle-right-mini svg {
    transform: rotate(0deg);
}

.sidebar-collapsed.sidebar-right-collapsed .designer-layout {
    grid-template-columns: 1fr;
}

.panel-section {
    display: grid;
    gap: 10px;
}

.canvas-wrap {
    position: relative;
    overflow: hidden;
    background: #fbfaf7;
}

.designer-canvas {
    position: relative;
    width: 100%;
    height: 100%;
    user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    touch-action: none;
}

.canvas-pan,
.canvas-zoom {
    position: absolute;
    inset: 0;
}

.canvas-zoom {
    transform-origin: 0 0;
    overflow: visible;
}

#connection-layer {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    pointer-events: auto;
    touch-action: none;
    overflow: visible;
}

#connection-layer path {
    pointer-events: stroke;
    cursor: pointer;
    stroke-opacity: 0.55;
}

#connection-layer path.active {
    stroke-width: 4;
    stroke-opacity: 0.9;
}

.tables-layer {
    position: absolute;
    inset: 0;
    z-index: 2;
}

.table-block {
    position: absolute;
    width: 240px;
    min-width: 240px;
    background: #fff;
    border-radius: 12px;
    border: 1px solid var(--line);
    box-shadow: var(--shadow-soft);
    overflow: hidden;
}

.table-header {
    padding: 10px 12px;
    background: #f0ebe1;
    border-radius: 12px 12px 0 0;
    font-weight: 700;
    cursor: move;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    touch-action: none;
}

.table-title {
    flex: 1;
}

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

.table-columns {
    padding: 10px 12px 12px;
    display: grid;
    gap: 6px;
    min-width: 0;
}

.column-item {
    padding: 6px 28px;
    border-radius: 8px;
    background: #f8f6f2;
    border: 1px solid #efe8db;
    cursor: pointer;
    font-size: 0.95rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    position: relative;
    width: 100%;
    min-width: 0;
}

.column-item.action-selected {
    border-color: rgba(11, 84, 61, 0.35);
    box-shadow: 0 0 0 2px rgba(11, 84, 61, 0.18);
    background: #eef6f1;
}

.table-block.selected {
    box-shadow: 0 0 0 2px rgba(11, 84, 61, 0.3), var(--shadow-soft);
}

.table-block.selected .table-header {
    background: #e4f1ea;
}

.column-item.dragging {
    opacity: 0.6;
    cursor: grabbing;
}

.column-name {
    flex: 1;
    cursor: pointer;
}

.column-actions {
    display: inline-flex;
    gap: 6px;
}

.column-handle {
    width: 12px;
    height: 12px;
    border-radius: 999px;
    border: 2px solid #f6f1e7;
    box-shadow: 0 0 0 1px rgba(11, 84, 61, 0.2);
    cursor: crosshair;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    touch-action: none;
}

.column-handle-in {
    left: 6px;
    background: #5f90d7;
}

.column-handle-out {
    right: 6px;
    background: #5fb581;
}

.column-handle:hover {
    filter: brightness(0.95);
}

.small {
    padding: 6px 10px;
    font-size: 0.85rem;
}

.tiny {
    padding: 4px 8px;
    font-size: 0.8rem;
}

.column-item.selected {
    outline: 2px solid var(--accent-2);
    background: #e8f3ee;
}

.column-item.column-focus {
    outline: 2px solid #5f90d7;
    background: #e8eef9;
}

.add-column {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 6px;
    margin-top: 8px;
}

.connection-list {
    display: grid;
    gap: 8px;
    font-size: 0.9rem;
}

.connection-item {
    padding: 8px 10px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.connection-item.active {
    border-color: #5f90d7;
    box-shadow: 0 0 0 1px rgba(95, 144, 215, 0.35);
}

.zoom-controls {
    display: grid;
    gap: 10px;
}

.zoom-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

#zoom-range {
    flex: 1;
}


@media (max-width: 900px) {
    .designer-layout {
        grid-template-columns: 1fr;
        grid-template-rows: auto 1fr auto;
    }
    .side-panel {
        border-right: none;
        border-left: none;
        border-bottom: 1px solid var(--line);
    }
    .side-panel-left {
        order: 1;
    }
    .canvas-wrap {
        order: 2;
    }
    .side-panel-right {
        order: 3;
    }
}

@media (max-width: 1024px) {
    .top-bar {
        flex-wrap: wrap;
        align-items: flex-start;
    }
    .top-bar > div:first-child {
        flex: 1 1 100%;
    }
    .table-toolbar {
        order: 2;
        width: 100%;
        justify-content: flex-start;
    }
    .voice-bar {
        order: 4;
        width: 100%;
        max-width: none;
        justify-content: flex-start;
    }
    .voice-bar input {
        max-width: none;
    }
    .top-bar nav {
        order: 3;
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        gap: 12px;
        justify-content: flex-start;
    }
}

@media (hover: none) and (pointer: coarse) {
    button {
        padding: 12px 16px;
    }
    .mic-btn,
    .copy-btn {
        width: 48px;
        height: 48px;
    }
    .side-toggle {
        width: 40px;
        height: 40px;
    }
    .table-header {
        padding: 12px 14px;
    }
    .column-handle {
        width: 18px;
        height: 18px;
    }
    .column-handle-in {
        left: 4px;
    }
    .column-handle-out {
        right: 4px;
    }
    .small {
        padding: 8px 12px;
        font-size: 0.9rem;
    }
    .tiny {
        padding: 6px 10px;
        font-size: 0.85rem;
    }
}

.modal {
    position: fixed;
    inset: 0;
    background: rgba(20, 18, 12, 0.35);
    display: grid;
    place-items: center;
    z-index: 10;
}

.modal.hidden {
    display: none;
}

.modal-content {
    width: min(800px, 92vw);
    background: #fff;
    padding: 20px;
    border-radius: 16px;
    box-shadow: var(--shadow);
    display: grid;
    gap: 12px;
}

.modal-content.small {
    width: min(480px, 92vw);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}
