/* Captain Claw — Datastore Browser */

:root {
    --bg-primary: #0d1117;
    --bg-secondary: #161b22;
    --bg-tertiary: #21262d;
    --bg-hover: #30363d;
    --border: #30363d;
    --text-primary: #e6edf3;
    --text-secondary: #8b949e;
    --text-muted: #6e7681;
    --accent: #58a6ff;
    --accent-hover: #79c0ff;
    --green: #3fb950;
    --red: #f85149;
    --yellow: #d29922;
    --orange: #f78166;
    --purple: #bc8cff;
    --radius: 8px;
    --radius-sm: 6px;
    --header-height: 48px;
    --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
    --font-mono: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
    height: 100%;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.5;
    overflow: hidden;
}

/* ─── Header ──────────────────────────────────────────── */

.ds-header {
    height: var(--header-height);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    flex-shrink: 0;
}

.ds-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.ds-header-left h1 {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
}

.ds-logo-link {
    display: flex;
    align-items: center;
    text-decoration: none;
}

.ds-header-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.ds-nav-link {
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 13px;
    padding: 4px 10px;
    border-radius: var(--radius-sm);
    transition: color 0.15s, background 0.15s;
}

.ds-nav-link:hover {
    color: var(--accent);
    background: var(--bg-tertiary);
}

/* ─── Layout ──────────────────────────────────────────── */

.ds-layout {
    display: flex;
    height: calc(100vh - var(--header-height));
    overflow: hidden;
}

/* ─── Sidebar ─────────────────────────────────────────── */

.ds-sidebar {
    width: 260px;
    min-width: 220px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    overflow: hidden;
}

.ds-sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.ds-sidebar-title {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
}

.ds-add-btn {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 18px;
    font-weight: 300;
    cursor: pointer;
    transition: background 0.15s;
    line-height: 1;
}

.ds-add-btn:hover { background: var(--accent-hover); }

.ds-table-list {
    flex: 1;
    overflow-y: auto;
}

.ds-table-list::-webkit-scrollbar { width: 6px; }
.ds-table-list::-webkit-scrollbar-track { background: transparent; }
.ds-table-list::-webkit-scrollbar-thumb { background: var(--bg-hover); border-radius: 3px; }

.ds-loading {
    padding: 24px 16px;
    text-align: center;
    color: var(--text-muted);
    font-size: 13px;
}

.ds-list-empty {
    padding: 32px 16px;
    text-align: center;
    color: var(--text-muted);
    font-size: 13px;
}

.ds-table-item {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 10px 14px;
    cursor: pointer;
    transition: background 0.1s;
    border-bottom: 1px solid var(--border);
    border-left: 3px solid transparent;
}

.ds-table-item:hover { background: var(--bg-hover); }

.ds-table-item.selected {
    background: var(--bg-tertiary);
    border-left-color: var(--accent);
}

.ds-table-item-name {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-primary);
    font-family: var(--font-mono);
}

.ds-table-item-meta {
    font-size: 11px;
    color: var(--text-muted);
}

/* ─── Main content ────────────────────────────────────── */

.ds-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
}

/* Empty state */
.ds-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    color: var(--text-muted);
}

.ds-empty-icon { font-size: 48px; opacity: 0.5; }
.ds-empty-text { font-size: 14px; }
.ds-empty-sub { font-size: 12px; opacity: 0.7; }

/* Table view */
.ds-table-view {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* ─── Toolbar ─────────────────────────────────────────── */

.ds-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
    flex-shrink: 0;
    gap: 12px;
}

.ds-toolbar-left {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.ds-table-name {
    font-size: 16px;
    font-weight: 600;
    font-family: var(--font-mono);
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ds-btn-icon {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 14px;
    padding: 2px 4px;
    border-radius: 4px;
    line-height: 1;
}
.ds-btn-icon:hover { color: var(--text-primary); background: var(--bg-hover); }
.ds-btn-icon.ds-btn-disabled,
.ds-btn-icon:disabled { opacity: 0.4; cursor: not-allowed; }

.ds-table-meta {
    font-size: 12px;
    color: var(--text-muted);
    white-space: nowrap;
}

.ds-toolbar-right {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

/* ─── Tabs ────────────────────────────────────────────── */

.ds-tab-group {
    display: flex;
    background: var(--bg-tertiary);
    border-radius: var(--radius-sm);
    overflow: hidden;
    border: 1px solid var(--border);
}

.ds-tab {
    padding: 5px 14px;
    font-size: 12px;
    font-family: var(--font);
    font-weight: 500;
    color: var(--text-secondary);
    background: transparent;
    border: none;
    cursor: pointer;
    transition: all 0.15s;
}

.ds-tab:hover { color: var(--text-primary); }

.ds-tab.active {
    background: var(--accent);
    color: #fff;
}

/* ─── Data grid ───────────────────────────────────────── */

.ds-tab-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.ds-grid-wrap {
    flex: 1;
    overflow: auto;
}

.ds-grid-wrap::-webkit-scrollbar { width: 6px; height: 6px; }
.ds-grid-wrap::-webkit-scrollbar-track { background: transparent; }
.ds-grid-wrap::-webkit-scrollbar-thumb { background: var(--bg-hover); border-radius: 3px; }

.ds-grid {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

.ds-grid thead {
    position: sticky;
    top: 0;
    z-index: 1;
}

.ds-grid th {
    background: var(--bg-secondary);
    border-bottom: 2px solid var(--border);
    padding: 8px 12px;
    text-align: left;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    color: var(--text-secondary);
    white-space: nowrap;
    user-select: none;
}

/* Sortable headers */
.ds-grid th.sortable {
    cursor: pointer;
    position: relative;
    padding-right: 22px;
    transition: color 0.15s, background 0.15s;
}

.ds-grid th.sortable:hover {
    color: var(--text-primary);
    background: var(--bg-tertiary);
}

.ds-grid th.sortable::after {
    content: '';
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 10px;
    opacity: 0.3;
}

.ds-grid th.sortable:hover::after {
    content: '\25B2';
    opacity: 0.5;
}

.ds-grid th.sort-asc {
    color: var(--accent);
}

.ds-grid th.sort-asc::after {
    content: '\25B2';
    opacity: 1;
    color: var(--accent);
}

.ds-grid th.sort-desc {
    color: var(--accent);
}

.ds-grid th.sort-desc::after {
    content: '\25BC';
    opacity: 1;
    color: var(--accent);
}

/* Filter row */
.ds-filter-row td {
    background: var(--bg-secondary);
    padding: 4px 6px;
    border-bottom: 1px solid var(--border);
}

.ds-filter-input {
    width: 100%;
    padding: 4px 8px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text-primary);
    font-family: var(--font-mono);
    font-size: 11px;
    outline: none;
    transition: border-color 0.15s;
    box-sizing: border-box;
}

.ds-filter-input:focus {
    border-color: var(--accent);
}

.ds-filter-input::placeholder {
    color: var(--text-muted);
    font-style: italic;
    font-family: var(--font);
}

.ds-grid td {
    padding: 6px 12px;
    border-bottom: 1px solid var(--border);
    color: var(--text-primary);
    white-space: nowrap;
    max-width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
    font-family: var(--font-mono);
    font-size: 12px;
}

.ds-grid tr:hover td {
    background: var(--bg-hover);
}

.ds-grid td.col-id {
    color: var(--text-muted);
    font-size: 11px;
    width: 60px;
}

.ds-grid th.col-id { width: 60px; }

.ds-grid td.null-val {
    color: var(--text-muted);
    font-style: italic;
}

.ds-grid td.null-val::after {
    content: 'NULL';
    font-size: 10px;
    opacity: 0.6;
}

.ds-grid td.json-val {
    color: var(--purple);
}

.ds-grid th.col-actions,
.ds-grid td.col-actions {
    width: 120px;
    text-align: right;
    white-space: nowrap;
}

.ds-grid-empty {
    text-align: center;
    color: var(--text-muted);
    padding: 32px 16px !important;
    font-style: italic;
    font-family: var(--font) !important;
}

.ds-row-action {
    padding: 2px 8px;
    font-size: 11px;
    font-family: var(--font);
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    margin-left: 4px;
    transition: all 0.15s;
}

.ds-row-action:hover {
    color: var(--text-primary);
    border-color: var(--text-muted);
}

.ds-row-action.danger:hover {
    color: var(--red);
    border-color: var(--red);
}

/* ─── Pagination ──────────────────────────────────────── */

.ds-pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 10px 16px;
    border-top: 1px solid var(--border);
    background: var(--bg-secondary);
    flex-shrink: 0;
}

.ds-page-info {
    font-size: 12px;
    color: var(--text-secondary);
    font-family: var(--font-mono);
}

/* ─── Schema view ─────────────────────────────────────── */

.ds-schema-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}

.ds-schema-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 20px;
    border-bottom: 1px solid var(--border);
}

.ds-schema-col-name {
    font-family: var(--font-mono);
    font-size: 13px;
    font-weight: 500;
    color: var(--text-primary);
    flex: 1;
}

.ds-schema-col-type {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    padding: 2px 10px;
    border-radius: 10px;
    background: var(--bg-tertiary);
    color: var(--accent);
    border: 1px solid var(--border);
}

.ds-schema-actions {
    padding: 12px 20px;
    flex-shrink: 0;
}

/* ─── Buttons ─────────────────────────────────────────── */

.ds-btn {
    padding: 6px 14px;
    border-radius: var(--radius-sm);
    font-size: 12px;
    font-family: var(--font);
    font-weight: 500;
    cursor: pointer;
    border: 1px solid var(--border);
    transition: all 0.15s;
    white-space: nowrap;
}

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

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

.ds-btn.secondary {
    background: transparent;
    color: var(--text-secondary);
}

.ds-btn.secondary:hover {
    color: var(--text-primary);
    border-color: var(--text-muted);
}

.ds-btn.danger {
    background: transparent;
    color: var(--red);
    border-color: var(--border);
}

.ds-btn.danger:hover {
    background: var(--red);
    color: #fff;
    border-color: var(--red);
}

.ds-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* ─── Modal ───────────────────────────────────────────── */

.ds-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.ds-modal {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
    min-width: 400px;
    max-width: 520px;
    max-height: 80vh;
    overflow-y: auto;
}

.ds-modal-wide {
    min-width: 500px;
    max-width: 640px;
}

.ds-modal-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 16px;
}

.ds-modal-body {
    margin-bottom: 20px;
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.6;
}

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

/* ─── Forms ───────────────────────────────────────────── */

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

.ds-form-label {
    display: block;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

.ds-form-label .required {
    color: var(--red);
    margin-left: 2px;
}

.ds-input, .ds-select {
    width: 100%;
    padding: 8px 12px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-family: var(--font);
    font-size: 13px;
    outline: none;
    transition: border-color 0.15s;
}

.ds-input:focus, .ds-select:focus {
    border-color: var(--accent);
}

.ds-input::placeholder { color: var(--text-muted); }

.ds-select {
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%238b949e' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 32px;
}

/* Column row (create table modal) */
.ds-col-row {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-bottom: 8px;
}

.ds-col-row .col-name {
    flex: 2;
}

.ds-col-row .col-type {
    flex: 1;
    min-width: 100px;
}

.ds-col-remove {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
    transition: all 0.15s;
    flex-shrink: 0;
}

.ds-col-remove:hover {
    color: var(--red);
    border-color: var(--red);
}

.ds-add-col-btn {
    margin-top: 4px;
}

/* ─── Toast ───────────────────────────────────────────── */

.ds-toast {
    position: fixed;
    bottom: 24px;
    right: 24px;
    padding: 10px 20px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 13px;
    color: var(--text-primary);
    z-index: 1001;
    opacity: 0;
    transform: translateY(8px);
    transition: opacity 0.2s, transform 0.2s;
    pointer-events: none;
}

.ds-toast.show { opacity: 1; transform: translateY(0); }
.ds-toast.success { border-color: var(--green); }
.ds-toast.error { border-color: var(--red); }

/* ─── Protections ────────────────────────────────────── */

/* Sidebar: protected table lock icon */
.ds-table-item.protected .ds-table-item-name::before {
    content: '\1F512 ';
    font-size: 11px;
}

/* Protected row highlight */
.ds-protected-row {
    background: rgba(210, 153, 34, 0.06);
}

.ds-protected-row:hover td {
    background: rgba(210, 153, 34, 0.10);
}

.ds-protected-row .ds-row-action:not(.ds-lock-btn) {
    /* Edit / Del buttons greyed out (handled via ds-btn-disabled) */
}

/* Protected cell indicator */
.ds-protected-cell {
    position: relative;
    background: rgba(210, 153, 34, 0.04);
    border-left: 2px solid var(--yellow) !important;
    cursor: context-menu;
}

/* Protected column header */
.ds-protected-col::before {
    content: '\1F512 ';
    font-size: 10px;
}

/* Protected column in schema view */
.ds-schema-row.ds-protected-col {
    background: rgba(210, 153, 34, 0.04);
    border-left: 3px solid var(--yellow);
}

/* Disabled button (protection-based) */
.ds-btn-disabled,
.ds-btn-disabled:hover {
    opacity: 0.35;
    cursor: not-allowed;
    pointer-events: none;
}

/* Lock button (row / column toggle) */
.ds-lock-btn {
    font-size: 12px;
    padding: 2px 6px;
    border-color: transparent;
    background: transparent;
    cursor: pointer;
    transition: all 0.15s;
    opacity: 0.5;
}

.ds-lock-btn:hover {
    opacity: 1;
    border-color: var(--border);
}

.ds-lock-btn.active {
    opacity: 1;
    background: rgba(210, 153, 34, 0.12);
    border-color: var(--yellow);
}

/* Protected input field in row modal */
.ds-input-protected {
    opacity: 0.45;
    cursor: not-allowed;
    border-color: var(--yellow) !important;
    background: rgba(210, 153, 34, 0.06);
}

/* ─── Protections modal ─────────────────────────────── */

.ds-protection-toggle {
    padding: 10px 0 14px 0;
    border-bottom: 1px solid var(--border);
    margin-bottom: 12px;
}

.ds-toggle-label {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    font-size: 13px;
    color: var(--text-primary);
}

.ds-toggle-label input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--yellow);
    cursor: pointer;
}

.ds-protection-list {
    max-height: 320px;
    overflow-y: auto;
}

.ds-protection-list::-webkit-scrollbar { width: 5px; }
.ds-protection-list::-webkit-scrollbar-track { background: transparent; }
.ds-protection-list::-webkit-scrollbar-thumb { background: var(--bg-hover); border-radius: 3px; }

.ds-protection-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 10px;
    border-bottom: 1px solid var(--border);
    gap: 12px;
}

.ds-protection-item:last-child {
    border-bottom: none;
}

.ds-protection-info {
    font-size: 12px;
    font-family: var(--font-mono);
    color: var(--text-secondary);
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ─── Responsive ──────────────────────────────────────── */

@media (max-width: 800px) {
    .ds-layout { flex-direction: column; }
    .ds-sidebar {
        width: 100%;
        min-width: unset;
        max-height: 180px;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }
    .ds-modal { min-width: 300px; max-width: 95vw; }
    .ds-modal-wide { min-width: 300px; max-width: 95vw; }
}

/* Export dropdown */
.ds-dropdown {
    position: relative;
    display: inline-block;
}
.ds-dropdown-menu {
    display: none;
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 4px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 6px;
    min-width: 140px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    z-index: 100;
    overflow: hidden;
}
.ds-dropdown-menu.open {
    display: block;
}
.ds-dropdown-item {
    display: block;
    width: 100%;
    padding: 8px 14px;
    border: none;
    background: none;
    color: var(--text-primary);
    font-size: 13px;
    text-align: left;
    cursor: pointer;
}
.ds-dropdown-item:hover {
    background: var(--accent);
    color: #fff;
}
