/* Improved base styles */
*:not([contenteditable]) {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

body {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial;
    margin: 0;
    color: #222;
    background: #f6f8fa;
}

header {
    background: #0b5cff;
    color: white;
    padding: 12px 18px;
    position: relative;
}

/* saved thumbnails in header (top-right) */
.saved-thumbs {
    position: absolute;
    right: 12px;
    top: 12px;
    display: flex;
    gap: 8px;
    align-items: center;
}
.saved-thumb {
    width: 60px;
    height: 60px;
    border-radius: 6px;
    overflow: hidden;
    background: #fff;
    border: 1px solid rgba(0,0,0,0.08);
    box-shadow: 0 2px 6px rgba(0,0,0,0.08);
    position: relative;
    cursor: pointer;
}
.saved-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.saved-thumb .thumb-delete {
    position: absolute;
    right: 4px;
    top: 4px;
    background: rgba(0,0,0,0.6);
    color: white;
    border-radius: 10px;
    width: 18px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    font-size: 12px;
    cursor: pointer;
}

main {
    display: flex;
    height: calc(100vh - 56px);
}

.menu {
    width: 220px;
    padding: 12px;
    border-right: 1px solid rgba(0, 0, 0, 0.06);
    background: linear-gradient(180deg, #fff, #f7f9fb);
}

.toolbar {
    display: flex;
    gap: 8px;
    flex-direction: column;
}

/* screen-reader only helper */
.sr-only {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

.toolbar .primary {
    background: #0b5cff;
    color: #fff;
    border: none;
    padding: 10px;
    border-radius: 6px;
    cursor: pointer;
}

.toolbar button {
    padding: 8px 10px;
    border-radius: 6px;
    border: 1px solid #d0d7de;
    background: #fff;
    cursor: pointer;
}

section#tables {
    position: relative;
    flex: 1 1 auto;
    overflow: auto;
    padding: 20px;
    background-image: linear-gradient(0deg, rgba(0, 0, 0, 0.02) 1px, transparent 1px), linear-gradient(90deg, rgba(0, 0, 0, 0.02) 1px, transparent 1px);
    background-size: 40px 40px, 40px 40px;
}

[contenteditable="true"] {
    cursor: text;
    user-select: text;
}

.table {
    border-radius: 8px;
    border: 1px solid rgba(0, 0, 0, 0.12);
    padding: 8px;
    position: absolute;
    top: 20px;
    left: 20px;
    background: linear-gradient(180deg, #fffef0, #fff9e6);
    min-width: 140px;
    box-shadow: 0 2px 6px rgba(51, 51, 51, 0.08);
    transition: box-shadow .12s ease, transform .06s ease;
}

.table.moving {
    cursor: grabbing;
    transform: translateZ(0);
    box-shadow: 6px 8px 18px rgba(51, 51, 51, 0.14);
}

.fields {
    min-height: 30px;
    margin: 10px 0;
    background: rgba(13, 110, 253, 0.04);
    border-radius: 6px;
    padding: 6px;
}

.title {
    background: white;
    padding: 8px;
    margin-bottom: 6px;
    border-radius: 6px;
    font-weight: 600;
}

table.fields {
    width: 100%;
    border-collapse: collapse;
}

tr.field td {
    padding: 6px 8px;
    vertical-align: middle;
}

td.label {
    min-width: 60px;
}

td.fieldType {
    min-width: 80px;
}

td.pkfk {
    min-width: 22px;
}

td.linkTo {
    min-width: 40px;
}

.removeField {
    color: #666;
    cursor: pointer;
    padding: 4px 6px;
    border-radius: 4px;
    border: 1px solid transparent;
    background: transparent;
}

.removeField:hover {
    background: rgba(0, 0, 0, 0.04);
    border-color: rgba(0, 0, 0, 0.06);
}

button.addField {
    margin-top: 6px;
    padding: 6px 8px;
    border-radius: 6px;
    border: 1px solid #d0d7de;
    background: #fff;
    cursor: pointer;
}

/* small responsive tweaks */
@media (max-width:700px) {
    .menu {
        display: none
    }
}

.table-top {
    display: flex;
    align-items: center;
    gap: 8px;
}

.drag-handle {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.04);
    color: #222;
    font-weight: 700;
    cursor: grab;
    user-select: none;
    flex: 0 0 auto;
}

.drag-handle:active {
    cursor: grabbing
}

.fk-handle {
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: #fff;
    border: 2px solid #0b5cff;
    color: #0b5cff;
    cursor: crosshair;
    margin-left: auto;
    flex: 0 0 auto;
}

/* delete table button in table header */
.delete-table {
    margin-left: 6px;
    background: rgba(0,0,0,0.06);
    border: 1px solid rgba(0,0,0,0.06);
    color: #222;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
}
.delete-table:hover { background: rgba(208,45,45,0.18); color: #d04545; }

.connections {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    overflow: visible;
}

.connections line {
    stroke: #0b5cff;
    stroke-width: 2;
    stroke-linecap: round;
}

.connections line.temp {
    stroke-dasharray: 6 6;
    opacity: 0.9
}

.link-handle {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 2px solid #0b5cff;
    background: white;
    display: inline-block;
    vertical-align: middle;
    cursor: crosshair;
    visibility: hidden;
}

.linkTo {
    text-align: center;
}

.pkfk.pk {
    font-weight: 700;
    color: #0b5cff;
    background: rgba(11, 92, 255, 0.06);
    border-radius: 4px;
    padding: 2px 6px;
}

.pkfk.fk {
    font-weight: 700;
    color: #d04545;
    background: rgba(208, 77, 77, 0.06);
    border-radius: 4px;
    padding: 2px 6px;
}

/* row reordering */
.row-drag { width:48px; text-align:center; }
.row-controls { display:flex; gap:6px; justify-content:center; }
.row-controls button { width:24px; height:24px; border-radius:4px; border:1px solid transparent; background: rgba(0,0,0,0.03); cursor:pointer; }
.row-controls button:active { transform: translateY(1px); }