/* Phase 8 module styling: grids, trees, editor, tabs, modal, toolbars. */

/* Wrap any wide table so it scrolls inside its own container instead of overflowing the page.
   When placed in a flex column (.view-fill) it fills the remaining height, so the body shows
   only the rows that fit, headers stay sticky, and a horizontal scrollbar sits at the bottom
   of the visible area. */
.table-scroll {
  width: 100%;
  max-width: 100%;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1;
  min-height: 0;
}
/* Tables in these scroll containers wrap long cell content (rows auto-grow), with a readable
   minimum column width preserved by the resize handles / min-width below. */
.table-scroll .grid-table tbody td {
  white-space: normal;
  word-break: break-word;
  overflow: visible;
  text-overflow: clip;
  vertical-align: top;
  min-width: 80px;
}
/* The audit-log fills its panel: filter bar on top, the scrollable table below. */
audit-log {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

/* Connections table: actions stay on one line on desktop, and the whole table scrolls
   horizontally on medium widths (via .table-scroll). On phones it collapses to stacked cards
   so every field is readable without horizontal scrolling. */
.conn-table .conn-actions {
  text-align: right;
  white-space: nowrap;
}
@media (max-width: 680px) {
  .conn-table thead {
    display: none; /* labels come from each cell's data-label instead */
  }
  .conn-table,
  .conn-table tbody,
  .conn-table tr,
  .conn-table td {
    display: block;
    width: 100%;
  }
  .conn-table tr {
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    margin: 10px 8px;
    background: var(--bg-surface);
    overflow: hidden;
  }
  .conn-table td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    text-align: right;
    padding: 8px 12px;
    border: none;
    border-bottom: 1px solid var(--border);
    min-width: 0;
  }
  .conn-table td:last-child {
    border-bottom: none;
  }
  .conn-table td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--text-muted);
    text-align: left;
    flex: 0 0 auto;
  }
  /* Actions wrap onto their own line, full-width tap targets. */
  .conn-table td.conn-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
    white-space: normal;
  }
  .conn-table td.conn-actions .btn {
    flex: 1 1 auto;
    min-width: 88px;
  }
}

/* Data grid (fallback table) */
.grid-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-sm);
}
/* Resizable tables keep their set column widths instead of auto-fitting. */
.resizable-table th,
.resizable-table td {
  overflow: hidden;
  text-overflow: ellipsis;
}
/* Let users select & copy cell text (some grids otherwise inherit user-select:none). */
.grid-table td,
.grid-table th .th-label {
  user-select: text;
}
.grid-table thead th {
  position: sticky;
  top: 0;
  background: var(--bg-elevated);
  text-align: left;
  font-weight: 600;
  color: var(--text-secondary);
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  white-space: nowrap;
}
.grid-table tbody td {
  padding: 6px var(--sp-3);
  border-bottom: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  max-width: 420px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.grid-table tbody tr:hover {
  background: var(--bg-hover);
}
/* Wrap mode: show full cell content, letting row height grow instead of truncating. */
.grid-table.wrap tbody td {
  white-space: normal;
  overflow: visible;
  text-overflow: clip;
  word-break: break-word;
  max-width: 360px;
  vertical-align: top;
}
.grid-table .null {
  color: var(--text-muted);
  font-style: italic;
}

.data-grid {
  display: flex;
  flex-direction: column;
  min-height: 0;
  flex: 1;
  position: relative;
}

/* Busy overlay + spinner */
.grid-busy {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  background: color-mix(in srgb, var(--bg-base) 68%, transparent);
  z-index: 5;
}
.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.spinner.sm {
  width: 13px;
  height: 13px;
  border-width: 2px;
  display: inline-block;
  vertical-align: -2px;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Resizable columns (fallback grid) */
.grid-table th {
  position: relative;
}
.grid-table th .th-label {
  cursor: pointer;
  display: inline-block;
}
.col-resize {
  position: absolute;
  top: 0;
  right: 0;
  width: 7px;
  height: 100%;
  cursor: col-resize;
  user-select: none;
}
.col-resize:hover {
  background: var(--accent-soft);
}
.grid-scroll {
  overflow: auto;
  flex: 1;
  min-height: 0;
}
.grid-empty {
  padding: var(--sp-5);
}
.grid-footer {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  border-top: 1px solid var(--border);
  background: var(--bg-surface);
}

/* Editor layout */
.editor-layout,
.schema-layout,
.viewer-layout {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.editor-toolbar {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  /* Wrap controls onto a second line instead of letting them overflow/clip on narrower
     desktop & laptop widths (e.g. ~1150px), so the Run button is always visible. */
  flex-wrap: wrap;
  row-gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
}
.code-editor {
  display: block;
  height: 240px;
  border-bottom: 1px solid var(--border);
}
/* Draggable split between the editor and the results/messages section. */
.editor-divider {
  flex: none;
  height: 7px;
  cursor: row-resize;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  position: relative;
}
.editor-divider::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 38px;
  height: 3px;
  border-radius: 999px;
  background: var(--border-strong);
}
.editor-divider:hover::after,
.editor-divider:focus-visible::after {
  background: var(--accent);
}
.code-fallback {
  width: 100%;
  height: 100%;
  border: none;
  border-radius: 0;
  resize: none;
  font-family: var(--font-mono);
  font-size: var(--fs-md);
  line-height: 1.6;
  background: var(--bg-base);
  padding: var(--sp-3);
}
.monaco-host {
  width: 100%;
  height: 100%;
}
.result-status {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
  font-size: var(--fs-sm);
}

/* Editor result tabs + messages */
.result-tabs {
  display: flex;
  gap: var(--sp-1);
  padding: 0 var(--sp-2);
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
}
.rtab {
  padding: var(--sp-2) var(--sp-3);
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: var(--fs-sm);
  cursor: pointer;
}
.rtab.active {
  color: var(--text-primary);
  border-bottom-color: var(--accent);
}
/* Results pane holding one or more result-set grids, stacked SSMS-style. */
.results-area {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: auto;
}
.results-area.hidden {
  display: none;
}
.result-block {
  display: flex;
  flex-direction: column;
  min-height: 0;
}
/* A single result set fills the whole pane (unchanged behaviour). */
.result-block.single {
  flex: 1;
}
/* Multiple result sets: each gets a bounded, individually-scrollable block, and the pane
   scrolls between them — exactly like SSMS's stacked "Results" grids. A draggable divider
   sits between adjacent blocks (see .result-divider) to resize them vertically. */
.result-block.multi {
  flex: none;
  height: 320px;
}
/* Row-resize handle between two stacked result grids. */
.result-divider {
  flex: none;
  height: 8px;
  cursor: row-resize;
  background: var(--bg-surface);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  position: relative;
  touch-action: none;
}
.result-divider::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 38px;
  height: 3px;
  border-radius: 999px;
  background: var(--border-strong);
}
.result-divider:hover::after {
  background: var(--accent);
}
.result-label {
  flex: none;
  display: flex;
  align-items: baseline;
  gap: var(--sp-2);
  padding: 4px var(--sp-3);
  font-size: var(--fs-sm);
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
}
.result-block .data-grid {
  min-height: 0;
}
.messages-panel {
  flex: 1;
  overflow: auto;
  min-height: 0;
  padding: var(--sp-2);
}
.msg-line {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 4px var(--sp-2);
  border-bottom: 1px solid var(--border);
  font-size: var(--fs-sm);
}
.msg-sql {
  font-family: var(--font-mono);
  color: var(--text-secondary);
}

/* Query-category badges */
.badge.cat-read {
  background: rgba(74, 158, 255, 0.16);
  color: var(--cat-read);
}
.badge.cat-write {
  background: rgba(214, 164, 23, 0.16);
  color: var(--cat-write);
}
.badge.cat-ddl {
  background: var(--danger-soft);
  color: var(--cat-ddl);
}

/* Session bar */
.session-bar {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  flex-wrap: wrap;
  row-gap: var(--sp-2);
}
.session-bar .input {
  width: auto;
  min-width: 150px;
}
.session-bar .sep {
  width: 1px;
  height: 22px;
  background: var(--border);
  margin: 0 var(--sp-1);
}

/* Viewer controls */
.viewer-controls {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
}
.viewer-controls .input {
  width: auto;
}

/* Schema tree */
.tree {
  overflow: auto;
  flex: 1;
  padding: var(--sp-2);
}
.tree-row {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 4px var(--sp-2);
  border-radius: var(--radius-sm);
  cursor: pointer;
  user-select: none;
}
.tree-row:hover {
  background: var(--bg-hover);
}
.tree-caret {
  width: 12px;
  color: var(--text-muted);
  font-size: 10px;
}
.tree-icon {
  color: var(--accent);
}
.tree-children {
  margin-left: 18px;
  border-left: 1px solid var(--border);
  padding-left: var(--sp-2);
}
.tree-leaf {
  display: flex;
  align-items: baseline;
  gap: var(--sp-2);
  padding: 3px var(--sp-2);
  font-size: var(--fs-sm);
}
.tree-meta {
  font-size: var(--fs-xs);
}
/* "View definition / ALTER script" action on a schema-tree object row. Hidden until the row is
   hovered/focused so the tree stays clean. */
.tree-action {
  margin-left: auto;
  padding: 0 6px;
  height: 20px;
  line-height: 18px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.12s;
}
.tree-row:hover .tree-action,
.tree-row:focus-within .tree-action,
.tree-leaf:hover .tree-action,
.tree-action:focus {
  opacity: 1;
}
.tree-action:hover {
  color: var(--accent);
  border-color: var(--accent);
}
.tree-leaf {
  position: relative;
}
.tree-loading {
  padding: 4px var(--sp-2);
  font-size: var(--fs-sm);
}

/* Tabs */
.tabs {
  display: flex;
  gap: var(--sp-1);
  border-bottom: 1px solid var(--border);
}
.tab {
  padding: var(--sp-2) var(--sp-4);
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}
.tab:hover {
  color: var(--text-secondary);
}
.tab.active {
  color: var(--text-primary);
  border-bottom-color: var(--accent);
}

/* Operations checkbox grid (access grants) */
.ops-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: var(--sp-2);
}
.op-check {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2);
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: var(--fs-sm);
  cursor: pointer;
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: grid;
  place-items: center;
  z-index: 1000;
  animation: fade-in var(--dur) var(--ease);
}
.modal {
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  max-height: 90vh;
  overflow: auto;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 1px solid var(--border);
}
.modal-head h3 {
  margin: 0;
}
.modal-body {
  padding: var(--sp-5);
}
.modal-form {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}
@keyframes fade-in {
  from {
    opacity: 0;
  }
}

/* TomSelect (access-grant DB/table pickers) themed to match app inputs. Uses CSS variables so
   it adapts to both the dark and light themes automatically. !important is needed because the
   vendored tom-select.css is injected AFTER the app stylesheets and would otherwise win. */
.ts-wrapper .ts-control,
.ts-wrapper.single .ts-control,
.ts-dropdown {
  background: var(--bg-elevated) !important;
  color: var(--text-primary) !important;
  border-color: var(--border-strong) !important;
  border-radius: var(--radius-md);
}
.ts-control,
.ts-control input,
.ts-control .item,
.ts-dropdown,
.ts-dropdown .option {
  color: var(--text-primary) !important;
}
.ts-control input::placeholder {
  color: var(--text-muted) !important;
}
.ts-dropdown {
  border: 1px solid var(--border-strong) !important;
  box-shadow: var(--shadow-lg);
}
.ts-dropdown .option.active,
.ts-dropdown .active,
.ts-dropdown .option:hover {
  background: var(--accent-soft) !important;
  color: var(--text-primary) !important;
}
.ts-wrapper.focus .ts-control {
  border-color: var(--accent) !important;
}
/* Selected tags in multi-select: accent-tinted, readable. */
.ts-wrapper.multi .ts-control > .item {
  background: var(--accent-soft) !important;
  color: var(--text-primary) !important;
  border: 1px solid var(--accent) !important;
  border-radius: var(--radius-sm);
}
.ts-wrapper.multi .ts-control > .item .remove {
  border-left-color: var(--accent) !important;
  color: var(--text-primary) !important;
}
.ts-wrapper.disabled .ts-control {
  opacity: 0.55;
}

/* Database diagram (ER) */
.erd-layout { display: flex; flex-direction: column; height: 100%; }
.erd-toolbar { flex-wrap: wrap; gap: var(--sp-2); }
.erd-tables { min-width: 220px; }
.erd-stage {
  position: relative;
  flex: 1;
  overflow: hidden;
  background:
    linear-gradient(var(--border) 1px, transparent 1px) 0 0 / 22px 22px,
    linear-gradient(90deg, var(--border) 1px, transparent 1px) 0 0 / 22px 22px,
    var(--bg-base);
  cursor: grab;
}
.erd-stage.panning { cursor: grabbing; }
.erd-canvas { position: absolute; top: 0; left: 0; transform-origin: 0 0; }
.erd-links { position: absolute; top: 0; left: 0; overflow: visible; pointer-events: none; }
.erd-link { stroke: var(--accent); stroke-width: 1.5; opacity: 0.8; }
.erd-one { fill: var(--accent); }
.erd-many { fill: var(--bg-base); stroke: var(--accent); stroke-width: 1.5; }
.erd-hint { position: absolute; top: 16px; left: 16px; }
.erd-box {
  position: absolute;
  width: 210px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  font-size: var(--fs-xs);
  user-select: none;
}
.erd-box-head {
  padding: 6px var(--sp-2);
  font-weight: 700;
  color: var(--accent-contrast);
  background: var(--accent);
  border-radius: var(--radius-md) var(--radius-md) 0 0;
  cursor: move;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.erd-box-body { max-height: 320px; overflow: auto; }
.erd-col {
  display: flex;
  justify-content: space-between;
  gap: var(--sp-2);
  padding: 3px var(--sp-2);
  border-top: 1px solid var(--border);
}
.erd-col-name { display: flex; align-items: center; gap: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.erd-col-type { color: var(--text-muted); font-family: var(--font-mono); white-space: nowrap; }
.erd-pk { font-size: 10px; }
.erd-fk { font-size: 9px; font-weight: 700; color: var(--accent); }

/* Diagram: resizable table-list panel + diagram workspace, context menu */
diagram-view { display: block; height: 100%; }
.erd-main { flex: 1; display: flex; min-height: 0; }
.erd-list {
  width: 240px;
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--bg-surface);
  min-width: 160px;
}
.erd-list-head {
  display: flex; gap: 8px; align-items: center;
  padding: 8px; border-bottom: 1px solid var(--border);
}
.erd-filter { flex: 1; min-width: 0; }
.erd-list-items { flex: 1; overflow: auto; min-height: 0; }
.erd-list-item {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 10px; cursor: pointer; font-size: var(--fs-sm);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  user-select: none;
}
.erd-list-item:hover { background: var(--bg-hover); }
.erd-list-item .erd-dot {
  width: 7px; height: 7px; border-radius: 50%;
  border: 1px solid var(--border-strong); flex: none;
}
.erd-list-item.on { color: var(--accent); font-weight: 600; }
.erd-list-item.on .erd-dot { background: var(--accent); border-color: var(--accent); }
.erd-resizer {
  width: 6px; flex: none; cursor: col-resize;
  background: var(--bg-surface); border-left: 1px solid var(--border);
  border-right: 1px solid var(--border); position: relative;
}
.erd-resizer::after {
  content: ""; position: absolute; left: 50%; top: 50%;
  transform: translate(-50%, -50%); width: 3px; height: 30px;
  border-radius: 999px; background: var(--border-strong);
}
.erd-resizer:hover::after { background: var(--accent); }
.erd-box.selected { outline: 2px solid var(--accent); outline-offset: 1px; }
.erd-menu {
  position: fixed; z-index: 1200; min-width: 210px;
  background: var(--bg-elevated); border: 1px solid var(--border-strong);
  border-radius: var(--radius-md); box-shadow: var(--shadow-lg);
  padding: 4px;
}
.erd-menu-item {
  display: block; width: 100%; text-align: left; padding: 7px 10px;
  background: none; border: none; color: var(--text-primary);
  font: inherit; cursor: pointer; border-radius: var(--radius-sm);
}
.erd-menu-item:hover { background: var(--accent-soft); }
