:root {
  --bg: #16181d;
  --bg-2: #1c1f26;
  --bg-3: #23272f;
  --border: #2d323c;
  --fg: #e4e7ec;
  --fg-dim: #9aa3b2;
  --accent: #f5b60d;
  --accent-dim: #c8930a;
  --danger: #ef5350;
  --ok: #4caf7d;
  --radius: 8px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

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

body {
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  height: 100vh;
  /* dvh tracks mobile browser chrome (URL bar) so the layout never overflows. */
  height: 100dvh;
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
}

.hidden { display: none !important; }
.muted { color: var(--fg-dim); }
.tiny { font-size: 12px; }
.spacer { flex: 1; }
.pad { padding: 24px; }

/* ---------------------------------------------------------- gate */

.gate {
  display: flex; align-items: center; justify-content: center;
  height: 100vh;
  height: 100dvh;
  padding: 16px;
}
.gate-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 36px;
  width: 340px;
  display: flex; flex-direction: column; gap: 14px;
  text-align: center;
}
.gate-logo { font-size: 40px; }
.gate-card h1 { font-size: 22px; font-weight: 600; }

/* ---------------------------------------------------------- layout */

.layout { display: flex; height: 100vh; height: 100dvh; }

.sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 12px;
}

.brand { display: flex; align-items: center; gap: 8px; font-size: 16px; font-weight: 600; }
.brand-mark { font-size: 20px; }

.section-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: .08em;
  color: var(--fg-dim); margin-top: 8px;
}

.conn-list { list-style: none; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 2px; }
.conn-item {
  padding: 9px 10px; border-radius: var(--radius); cursor: pointer;
  display: flex; align-items: center; gap: 8px;
  border: 1px solid transparent;
}
.conn-item:hover { background: var(--bg-3); }
.conn-item.active { background: var(--bg-3); border-color: var(--accent-dim); }
.conn-item .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--fg-dim); flex-shrink: 0; }
.conn-item.active .dot { background: var(--accent); }
.conn-meta { flex: 1; min-width: 0; }
.conn-name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conn-sub { font-size: 11px; color: var(--fg-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conn-actions { display: flex; gap: 2px; opacity: 0; }
.conn-item:hover .conn-actions { opacity: 1; }
.icon-btn {
  background: none; border: none; color: var(--fg-dim); cursor: pointer;
  padding: 2px 5px; border-radius: 4px; font-size: 13px;
}
.icon-btn:hover { background: var(--bg); color: var(--fg); }

.sidebar-foot { border-top: 1px solid var(--border); padding-top: 10px; }

/* ---------------------------------------------------------- main */

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

.empty {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 8px;
}
.empty-mark { font-size: 44px; opacity: .5; }
.empty h2 { font-size: 17px; font-weight: 500; }

.workspace { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  background: var(--bg-2);
}
.topbar-left { display: flex; align-items: center; gap: 8px; min-width: 0; }
.badge {
  background: var(--bg-3); border: 1px solid var(--border);
  padding: 2px 7px; border-radius: 4px; font-size: 11px; color: var(--fg-dim);
}

.tabs { margin-left: auto; display: flex; gap: 4px; }
.tab {
  background: none; border: 1px solid transparent; color: var(--fg-dim);
  padding: 6px 14px; border-radius: var(--radius); cursor: pointer; font-size: 13px;
}
.tab:hover { color: var(--fg); }
.tab.active { background: var(--bg-3); color: var(--fg); border-color: var(--border); }

.pane { flex: 1; min-height: 0; min-width: 0; display: flex; flex-direction: column; }

/* ---------------------------------------------------------- browse */

.browse-split { flex: 1; display: flex; min-height: 0; }

.table-list {
  width: 240px; flex-shrink: 0; border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 10px; gap: 8px;
}
.table-list ul { list-style: none; overflow-y: auto; flex: 1; }
.table-list li {
  padding: 6px 8px; border-radius: 5px; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font-size: 13px;
}
.table-list li:hover { background: var(--bg-3); }
.table-list li.active { background: var(--bg-3); color: var(--accent); }
.table-list li .cnt { float: right; color: var(--fg-dim); font-size: 11px; }

.table-data { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.data-head {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
}

/* Gutter lives on the scroll wrapper's inner content, not the wrapper itself,
   so sibling strips (toolbar, status) keep a consistent right edge. */
.grid-wrap { flex: 1; overflow: auto; }
.grid-wrap > table.grid { margin: 0 16px 16px 16px; }

/* width:max-content sizes columns to their content instead of stretching one
   column to fill the pane, and lets the table terminate with a visible edge. */
table.grid {
  border-collapse: collapse; width: max-content;
  font-family: var(--mono); font-size: 12.5px;
  border: 1px solid var(--border); border-radius: 6px;
}
table.grid th:last-child, table.grid td:last-child { border-right: none; }
table.grid tr:last-child td { border-bottom: none; }
table.grid th {
  position: sticky; top: 0; z-index: 1;
  background: var(--bg-3); text-align: left;
  padding: 8px 12px; border-bottom: 1px solid var(--border); border-right: 1px solid var(--border);
  font-weight: 600; white-space: nowrap;
}
table.grid td {
  padding: 6px 12px; border-bottom: 1px solid var(--border); border-right: 1px solid var(--border);
  max-width: 420px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
table.grid tr:hover td { background: var(--bg-2); }
td.null { color: var(--fg-dim); font-style: italic; }

/* ---------------------------------------------------------- query */

.query-head { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border); }

#sql {
  height: 190px; resize: vertical;
  background: var(--bg-2); color: var(--fg);
  border: none; border-bottom: 1px solid var(--border);
  padding: 14px 16px; font-family: var(--mono); font-size: 13px; outline: none;
  width: 100%; display: block;
}

.query-results { flex: 1; overflow: auto; }
.query-results table.grid { margin: 0 16px 16px 16px; }

.result-meta {
  padding: 8px 16px; border-bottom: 1px solid var(--border);
  color: var(--fg-dim); font-size: 12px; background: var(--bg-2);
}

/* ---------------------------------------------------------- forms */

.btn {
  background: var(--bg-3); color: var(--fg);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 8px 14px; cursor: pointer; font-size: 13px;
}
.btn:hover { border-color: var(--fg-dim); }
.btn.primary { background: var(--accent); color: #1a1a1a; border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { background: var(--accent-dim); }
.btn.ghost { background: none; }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.full { width: 100%; }
.btn:disabled { opacity: .4; cursor: not-allowed; }

input, select, textarea {
  background: var(--bg); color: var(--fg);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 8px 10px; font-size: 13px; width: 100%; font-family: inherit;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent-dim); }

/* The native checkbox renders as a white box that clashes with the dark
   theme and reads as pre-checked. Draw it from scratch instead. */
input[type="checkbox"] {
  appearance: none; -webkit-appearance: none;
  width: 18px; height: 18px; flex: none;
  border: 1px solid var(--border); border-radius: 4px;
  background: var(--bg); cursor: pointer;
  display: inline-grid; place-content: center;
}
input[type="checkbox"]:checked { background: var(--accent); border-color: var(--accent); }
input[type="checkbox"]:checked::after {
  content: ""; width: 9px; height: 5px;
  border: 2px solid #1a1a1a; border-top: 0; border-right: 0;
  transform: rotate(-45deg) translateY(-1px);
}
input[type="checkbox"]:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.6);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.modal {
  background: var(--bg-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 24px; width: 460px; max-height: 90vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 13px;
}
.modal h2 { font-size: 17px; font-weight: 600; }
.modal label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--fg-dim); }
.modal label.check { flex-direction: row; align-items: center; gap: 8px; }
.modal label.check input { width: auto; }
.row { display: flex; gap: 10px; }
.row .grow { flex: 1; }
.row .port { width: 100px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 4px; }

.error { color: var(--danger); font-size: 12.5px; }
.test-ok { color: var(--ok); font-size: 12.5px; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: var(--fg-dim); }

/* ------------------------------------------------------- responsive */

/* Hamburger and "‹ Tables" exist only in the narrow layout. */
.menu-btn, .back-btn { display: none; }

.drawer-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 29;
}

/* Tablet: reclaim horizontal space before switching to drawers. */
@media (max-width: 1024px) {
  .sidebar { width: 210px; }
  .table-list { width: 190px; }
}

/* Phone / small tablet: sidebar becomes an off-canvas drawer and the
   browse pane becomes a two-step list -> data flow, so the grid gets the
   full viewport width instead of the ~50px a 3-column layout would leave. */
@media (max-width: 760px) {
  .menu-btn { display: inline-flex; align-items: center; justify-content: center;
              font-size: 18px; padding: 4px 8px; flex-shrink: 0; }

  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 30;
    width: 82vw; max-width: 320px;
    transform: translateX(-100%);
    transition: transform .22s ease;
    /* Respect notches when the drawer is flush to the screen edge. */
    padding-left: max(16px, env(safe-area-inset-left));
  }
  .sidebar.open { transform: translateX(0); }

  /* Keep the connection name readable; hover-reveal never fires on touch. */
  .conn-actions { opacity: 1; }

  .topbar { gap: 8px; padding: 10px 12px; }
  /* The host string is the first thing worth dropping when space is tight. */
  #active-target { display: none; }
  .topbar-left { flex: 1; overflow: hidden; }
  #active-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .tabs { margin-left: 0; flex-shrink: 0; }
  .tab { padding: 6px 10px; }

  /* Two-step browse: table list fills the pane, data slides in over it. */
  .browse-split { position: relative; }
  .table-list {
    width: 100%; border-right: none;
    position: absolute; inset: 0; z-index: 1;
  }
  .table-data {
    position: absolute; inset: 0; z-index: 2;
    background: var(--bg);
    transform: translateX(100%);
    transition: transform .22s ease;
  }
  .browse-split.show-data .table-data { transform: translateX(0); }
  .back-btn { display: inline-flex; flex-shrink: 0; }

  .data-head { padding: 10px 12px; gap: 6px; flex-wrap: nowrap; }
  /* Row count is redundant next to Prev/Next when space is scarce. */
  #row-info { display: none; }
  /* Without min-width:0 a long table name refuses to shrink and wraps the
     toolbar onto a second line, pushing Prev/Next out of reach. */
  #data-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .data-head .spacer { display: none; }
  .data-head .btn { flex-shrink: 0; }

  .grid-wrap > table.grid, .query-results table.grid { margin: 0 12px 12px 12px; }
  /* Long values should not force a wide horizontal scroll on a phone. */
  table.grid td { max-width: 60vw; }

  .query-head { padding: 10px 12px; }
  #sql { height: 150px; padding: 12px; }

  /* Full-bleed sheet reads as native on a phone; 16px font stops iOS zoom. */
  .modal {
    width: 100%; height: 100%; max-height: 100%;
    border-radius: 0; border: none;
    padding: 20px max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
  }
  /* Inputs only - a blanket min-height would also inflate the SSL checkbox. */
  .modal input:not([type="checkbox"]), .modal select {
    font-size: 16px; padding: 11px 12px; min-height: 44px;
  }
  /* Label text and its hint share one baseline row rather than stacking. */
  .label-row { display: block; }
  /* A default 12px checkbox is far under the 44px touch minimum; enlarge the
     control and make the whole row tappable. */
  .modal label.check { min-height: 44px; align-items: center; cursor: pointer; }
  .modal label.check input[type="checkbox"] {
    width: 22px; height: 22px; flex-shrink: 0; accent-color: var(--accent);
  }
  /* Footer carries the bottom safe-area inset so Save clears the iOS home
     indicator while the scrolling form above it stays flush. */
  .modal-actions { position: sticky; bottom: 0; background: var(--bg-2);
                   padding-top: 12px; margin-top: auto;
                   padding-bottom: max(16px, env(safe-area-inset-bottom)); }
  .modal-actions .btn { flex: 1; padding: 13px 14px; min-height: 44px; }

  .gate-card { width: 100%; max-width: 340px; margin: 0 16px; padding: 28px 22px; }
  .gate-card input { font-size: 16px; padding: 10px 12px; }

  /* Touch targets: 36px+ so taps land reliably. */
  .conn-item { padding: 11px 10px; }
  .table-list li { padding: 10px 8px; font-size: 14px; }
  .btn.small { padding: 7px 11px; }
}

/* Very narrow: drop the wordmark so the hamburger and tabs always fit. */
@media (max-width: 400px) {
  .brand-name { font-size: 15px; }
  .tab { padding: 6px 8px; font-size: 12px; }
  table.grid td { max-width: 50vw; }
}

/* Landscape phones are short, not narrow: reclaim vertical space. */
@media (max-height: 460px) and (max-width: 900px) {
  #sql { height: 90px; }
  .modal { padding-top: 14px; }
}
