:root {
  color-scheme: light;
  --bg: #ffffff;
  --fg: #24292f;
  --muted: #57606a;
  --subtle: #6e7781;
  --canvas-subtle: #f6f8fa;
  --canvas-inset: #f6f8fa;
  --border: #d0d7de;
  --border-muted: #d8dee4;
  --accent: #0969da;
  --accent-fg: #ffffff;
  --accent-muted: #ddf4ff;
  --hover: #f3f4f6;
  --shadow: 0 8px 24px rgba(140, 149, 159, 0.2);
  --radius: 8px;
  --control-height: 38px;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d1117;
  --fg: #c9d1d9;
  --muted: #8b949e;
  --subtle: #6e7681;
  --canvas-subtle: #161b22;
  --canvas-inset: #010409;
  --border: #30363d;
  --border-muted: #21262d;
  --accent: #58a6ff;
  --accent-fg: #0d1117;
  --accent-muted: #1f6feb26;
  --hover: #21262d;
  --shadow: 0 8px 24px rgba(1, 4, 9, 0.55);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--fg);
}

button {
  appearance: none;
  font: inherit;
}

.app-shell {
  display: grid;
  min-height: 100vh;
  grid-template-columns: 280px 1fr;
  background: var(--bg);
}

.sidebar {
  position: sticky;
  top: 0;
  display: grid;
  height: 100vh;
  grid-template-rows: auto 1fr;
  border-right: 1px solid var(--border);
  background: var(--canvas-subtle);
  overflow: hidden;
}

.app-title {
  display: flex;
  align-items: center;
  min-height: 72px;
  padding: 18px;
  border-bottom: 1px solid var(--border);
}

.title-main,
.title-sub {
  display: block;
}

.title-main {
  font-size: 18px;
  font-weight: 700;
}

.title-sub {
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.db-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 0;
  overflow: auto;
  padding: 12px;
}

.db-group {
  overflow: hidden;
  border: 1px solid transparent;
  border-radius: var(--radius);
}

.db-group[data-open="true"] {
  border-color: var(--border);
  background: var(--bg);
  box-shadow: var(--shadow);
}

.db-row {
  display: grid;
  width: 100%;
  grid-template-columns: 24px 1fr 28px;
  align-items: center;
  gap: 10px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--fg);
  cursor: pointer;
  padding: 10px;
  text-align: left;
}

.db-row:hover {
  background: var(--hover);
}

.db-group[data-open="true"] .db-row {
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
}

.db-icon {
  display: grid;
  width: 24px;
  height: 24px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--accent);
  font-weight: 700;
}

.db-name {
  font-size: 14px;
  font-weight: 600;
}

.db-count {
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.add-button {
  display: grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--accent);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}

.add-button:hover {
  background: var(--accent-muted);
}

.connection-list {
  display: none;
  border-top: 1px solid var(--border-muted);
  padding: 6px 10px 6px 6px;
}

.db-group[data-open="true"] .connection-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.connection-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 92px;
  align-items: center;
  gap: 10px;
  border-radius: 6px;
}

.connection-row:hover,
.connection-row[data-selected="true"] {
  background: var(--accent-muted);
}

.connection-item {
  min-width: 0;
  width: 100%;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--fg);
  cursor: pointer;
  padding: 8px 10px;
  text-align: left;
}

.connection-actions {
  display: flex;
  gap: 4px;
}

.query-connection-button,
.edit-button,
.delete-connection-button {
  display: grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--muted);
  cursor: pointer;
  padding: 0;
  font-size: 12px;
  font-weight: 600;
  line-height: 1;
}

.query-connection-button:hover,
.edit-button:hover,
.delete-connection-button:hover {
  color: var(--accent);
  border-color: var(--accent);
}

.connection-title {
  display: block;
  font-size: 13px;
  font-weight: 600;
}

.connection-meta {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.empty-connections {
  padding: 10px;
  color: var(--muted);
  font-size: 12px;
}

.workspace {
  min-width: 0;
  padding: 24px;
}

.workspace-topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 24px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 24px;
}

.workspace-topbar p,
.empty-panel p,
.detail-card p {
  margin-top: 6px;
  color: var(--muted);
}

.theme-control {
  display: flex;
  gap: 4px;
  height: var(--control-height);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--canvas-subtle);
  padding: 0;
}

.theme-button {
  display: grid;
  height: 100%;
  place-items: center;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 0 10px;
  line-height: 1;
}

.theme-button:hover {
  color: var(--fg);
}

.theme-button[data-active="true"] {
  background: var(--accent);
  color: var(--accent-fg);
}

.empty-panel,
.detail-card {
  max-width: 720px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--canvas-subtle);
  padding: 24px;
}

.preview-panel {
  display: grid;
  gap: 18px;
}

.preview-loading {
  color: var(--muted);
  font-size: 14px;
}

.preview-table {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--canvas-subtle);
  overflow: hidden;
}

.preview-table-title {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  border-bottom: 1px solid var(--border);
  padding: 9px 12px;
}

.preview-table-title h2 {
  font-size: 16px;
  line-height: 1.2;
}

.preview-table-title span {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.2;
}

.preview-range-form {
  display: flex;
  align-items: end;
  gap: 8px;
}

.preview-range-form label {
  position: relative;
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.preview-range-form label span {
  position: absolute;
  top: -5px;
  right: 8px;
  background: var(--canvas-subtle);
  padding: 0 4px;
  font-size: 10px;
  line-height: 1;
}

.preview-range-form input {
  width: 92px;
  height: var(--control-height);
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  padding: 0 8px;
}

.preview-range-form > .secondary-action {
  box-sizing: border-box;
  display: inline-grid;
  min-width: 64px;
  height: var(--control-height);
  place-items: center;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
  font: inherit;
  padding: 0 12px;
}

.table-scroll {
  max-height: 460px;
  overflow: auto;
}

table {
  width: 100%;
  min-width: 640px;
  border-collapse: collapse;
  font-size: 13px;
}

th,
td {
  max-width: 360px;
  border-bottom: 1px solid var(--border-muted);
  padding: 8px 10px;
  text-align: left;
  vertical-align: top;
  overflow-wrap: anywhere;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  color: var(--muted);
  background: var(--bg);
  font-weight: 600;
}

.row-action-heading,
.row-actions {
  position: sticky;
  right: 0;
  width: 72px;
  min-width: 72px;
  max-width: 72px;
  background: var(--bg);
  text-align: center;
  white-space: nowrap;
}

.row-action-heading {
  z-index: 2;
}

.row-actions {
  z-index: 1;
}

.row-action {
  display: inline-grid;
  width: 26px;
  height: 26px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  line-height: 1;
}

.row-action:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.row-readonly {
  color: var(--subtle);
  font-size: 12px;
}

.row-edit-input {
  width: 100%;
  min-width: 120px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  font: inherit;
  padding: 6px 8px;
}

.detail-grid {
  display: grid;
  grid-template-columns: 120px 1fr;
  gap: 10px;
  margin-top: 18px;
  font-size: 14px;
}

.detail-label {
  color: var(--muted);
}

.detail-value {
  overflow-wrap: anywhere;
}

.connection-form {
  display: grid;
  gap: 16px;
  margin-top: 20px;
}

.field {
  display: grid;
  gap: 6px;
}

.field span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
}

.field input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--fg);
  padding: 10px 12px;
}

.field input:focus {
  border-color: var(--accent);
  outline: 2px solid var(--accent-muted);
}

.form-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.form-actions > button {
  all: unset;
  box-sizing: border-box;
  display: inline-flex;
  flex: 0 0 auto;
  height: 38px;
  min-height: 38px;
  max-height: 38px;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  font: inherit;
  padding: 0 12px;
  line-height: 1;
}

.form-actions > button:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

.form-actions > .primary-action {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-fg);
}

.form-actions > .secondary-action {
  border-color: var(--border);
  background: var(--bg);
  color: var(--fg);
}

.form-message {
  min-height: 18px;
  color: #cf222e;
  font-size: 13px;
}

.form-message[data-tone="success"] {
  color: #1a7f37;
}

.query-modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  align-items: start;
  justify-items: center;
  background: rgba(1, 4, 9, 0.55);
  padding: 23vh 24px 24px;
}

.query-modal {
  display: grid;
  width: min(960px, 100%);
  max-height: min(760px, calc(100vh - 48px));
  grid-template-rows: auto minmax(120px, 220px) auto minmax(0, 1fr);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.query-modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--border);
  padding: 14px 16px;
}

.query-modal-header h2 {
  font-size: 16px;
}

.query-modal-header p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.query-modal-close {
  display: grid;
  width: 30px;
  height: 30px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}

.query-editor {
  width: 100%;
  min-height: 120px;
  resize: none;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: var(--canvas-inset);
  color: var(--fg);
  font: 13px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  line-height: 1.5;
  outline: 0;
  padding: 12px 14px;
}

.query-status {
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
  padding: 9px 14px;
}

.query-status[data-tone="success"] {
  color: #1a7f37;
}

.query-status[data-tone="error"] {
  color: #cf222e;
}

.query-result {
  min-height: 0;
  overflow: auto;
}

.query-result-scroll {
  overflow: auto;
}

.query-empty-result {
  color: var(--muted);
  font-size: 13px;
  padding: 14px;
}

.login-backdrop {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  align-items: start;
  justify-items: center;
  background: rgba(1, 4, 9, 0.65);
  padding: 23vh 24px 24px;
}

.login-widget {
  display: grid;
  width: min(360px, 100%);
  gap: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  box-shadow: var(--shadow);
  padding: 22px;
}

.login-widget h2 {
  font-size: 18px;
}

.login-submit {
  display: grid;
  height: var(--control-height);
  place-items: center;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-fg);
  cursor: pointer;
  font: inherit;
}

:root[data-theme="dark"] .form-message {
  color: #ff7b72;
}

:root[data-theme="dark"] .form-message[data-tone="success"] {
  color: #3fb950;
}

:root[data-theme="dark"] .query-status[data-tone="success"] {
  color: #3fb950;
}

:root[data-theme="dark"] .query-status[data-tone="error"] {
  color: #ff7b72;
}

@media (max-width: 760px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    height: auto;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  .db-list {
    max-height: 50vh;
  }

  .workspace-topbar {
    flex-direction: column;
  }

  .theme-control {
    align-self: stretch;
  }

  .theme-button {
    flex: 1;
  }

  .preview-table-title {
    align-items: stretch;
    flex-direction: column;
  }

  .preview-range-form {
    align-items: stretch;
  }

  .preview-range-form label {
    flex: 1;
  }

  .preview-range-form input {
    width: 100%;
  }
}
