:root {
  color-scheme: dark;
  --bg: #090b0e;
  --surface: #10141a;
  --surface-2: #151a21;
  --surface-3: #1a2028;
  --border: #29313b;
  --border-strong: #3a4654;
  --text: #edf2f7;
  --muted: #8d99a8;
  --muted-2: #687484;
  --accent: #4f9cff;
  --accent-strong: #2f83ef;
  --success: #55b989;
  --warning: #d7aa55;
  --danger: #d36b72;
  --focus: #8cc0ff;
  --font: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --mono: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}

* {
  box-sizing: border-box;
}

html {
  background: var(--bg);
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
}

button,
input,
select {
  font: inherit;
}

button,
select {
  cursor: pointer;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
a:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.topbar {
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: #0d1014;
}

.identity {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
}

.product-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border: 1px solid var(--accent);
  color: var(--accent);
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 700;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 17px;
  line-height: 1.25;
  font-weight: 680;
}

h2 {
  font-size: 15px;
  line-height: 1.25;
  font-weight: 650;
}

.identity p,
.selection-strip span:last-child {
  color: var(--muted);
  font-size: 12px;
}

.service-state {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  white-space: nowrap;
}

.service-state a {
  margin-left: 10px;
  color: var(--accent);
  text-decoration: none;
}

.service-state a:hover {
  text-decoration: underline;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted-2);
}

.status-dot--ok { background: var(--success); }
.status-dot--error { background: var(--danger); }

.safety-notice {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 36px;
  padding: 7px 16px;
  border-bottom: 1px solid #4f4228;
  background: #18150e;
  color: #cbbf9d;
  font-size: 12px;
}

.safety-notice strong {
  color: var(--warning);
  font-family: var(--mono);
  font-size: 11px;
}

.app-shell {
  min-height: calc(100vh - 105px);
  display: grid;
  grid-template-columns: 304px minmax(0, 1fr);
}

.database-panel {
  min-width: 0;
  padding: 16px 14px;
  border-right: 1px solid var(--border);
  background: #0d1014;
}

.section-header,
.work-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.eyebrow,
.label {
  margin-bottom: 3px;
  color: var(--muted-2);
  font-family: var(--mono);
  font-size: 10px;
  line-height: 1.2;
  text-transform: uppercase;
}

.catalog-summary {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px;
  margin: 12px 0 8px;
  padding: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-size: 11px;
}

#database-count {
  color: var(--text);
  font-family: var(--mono);
}

#catalog-root {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.database-list {
  display: grid;
  gap: 6px;
}

.database-item {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 5px 8px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 0;
  background: var(--surface);
  color: var(--text);
  text-align: left;
}

.database-item:hover:not(:disabled) {
  border-color: var(--border-strong);
  background: var(--surface-2);
}

.database-item.is-selected {
  border-color: var(--accent);
  box-shadow: inset 2px 0 0 var(--accent);
}

.database-item:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.database-name {
  overflow: hidden;
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 700;
  text-overflow: ellipsis;
}

.database-status {
  color: var(--success);
  font-size: 10px;
  text-transform: uppercase;
}

.database-status.is-invalid { color: var(--danger); }

.database-meta,
.database-path {
  grid-column: 1 / -1;
  overflow: hidden;
  color: var(--muted);
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.database-path {
  color: var(--muted-2);
  font-family: var(--mono);
  font-size: 10px;
}

.catalog-issues {
  margin-top: 10px;
  padding: 9px;
  border: 1px solid #53363a;
  background: #1a1113;
  color: #d8a8ac;
  font-size: 11px;
}

.empty-state {
  padding: 15px 8px;
  border: 1px dashed var(--border);
  color: var(--muted);
  text-align: center;
}

.work-area {
  min-width: 0;
  padding: 18px;
}

.work-toolbar {
  min-height: 40px;
  margin-bottom: 14px;
}

.operation-switch {
  display: flex;
  border: 1px solid var(--border);
  background: var(--surface);
}

.operation-button {
  min-width: 90px;
  min-height: 38px;
  padding: 7px 14px;
  border: 0;
  border-right: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  font-weight: 600;
}

.operation-button:last-child { border-right: 0; }

.operation-button:hover { color: var(--text); }

.operation-button.is-active {
  background: var(--surface-3);
  color: var(--accent);
  box-shadow: inset 0 -2px 0 var(--accent);
}

.selection-strip {
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr);
  align-items: baseline;
  gap: 8px 12px;
  margin-bottom: 14px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  background: var(--surface);
}

.selection-strip .label { margin: 0; }

#selected-database {
  font-family: var(--mono);
  font-size: 12px;
}

.workspace-grid {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(520px, 1fr) minmax(320px, 0.62fr);
  gap: 14px;
  align-items: start;
}

.calculation-form,
.results-panel {
  min-width: 0;
  border: 1px solid var(--border);
  background: var(--surface);
}

fieldset {
  margin: 0;
  padding: 0 14px 14px;
  border: 0;
  border-bottom: 1px solid var(--border);
}

legend {
  width: 100%;
  margin-bottom: 10px;
  padding: 14px 0 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.field-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
}

.field-grid + .field-grid { margin-top: 12px; }
.field-grid--3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.field-grid--4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }

.field {
  min-width: 0;
  display: grid;
  align-content: start;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
}

.field > span,
.field > label {
  min-height: 16px;
}

.field small {
  color: var(--muted-2);
  font-size: 10px;
}

input,
select {
  width: 100%;
  height: 38px;
  padding: 7px 9px;
  border: 1px solid var(--border-strong);
  border-radius: 0;
  background: #0c1015;
  color: var(--text);
  font-size: 13px;
}

select {
  appearance: none;
  padding-right: 32px;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position:
    calc(100% - 20px) 50%,
    calc(100% - 15px) 50%;
  background-repeat: no-repeat;
  background-size: 5px 5px;
}

.measurement-control {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}

.measurement-control input {
  min-width: 0;
}

.measurement-control .unit-select {
  width: auto;
  min-width: 66px;
  border-left: 0;
  color: var(--accent);
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 700;
}

.measurement-control:focus-within {
  box-shadow: 0 0 0 1px var(--focus);
}

.measurement-control input:focus-visible,
.measurement-control select:focus-visible {
  position: relative;
  z-index: 1;
}

input:hover,
select:hover { border-color: #536173; }

input:invalid:not(:focus) { border-color: #684147; }

.form-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px;
}

.form-actions p {
  color: var(--muted);
  font-size: 12px;
}

.button {
  min-height: 36px;
  padding: 7px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 0;
  background: var(--surface-2);
  color: var(--text);
  font-weight: 600;
}

.button:hover:not(:disabled) { border-color: #59687a; }
.button:disabled { cursor: not-allowed; opacity: 0.45; }
.button--quiet { min-height: 32px; color: var(--muted); font-size: 11px; }

.button--primary {
  min-width: 160px;
  border-color: var(--accent-strong);
  background: var(--accent-strong);
  color: #fff;
}

.button--primary:hover:not(:disabled) { background: var(--accent); }

.results-panel {
  position: sticky;
  top: 10px;
}

.section-header--results {
  padding: 13px;
  border-bottom: 1px solid var(--border);
}

.result-badge {
  padding: 3px 7px;
  border: 1px solid var(--border-strong);
  color: var(--muted);
  font-family: var(--mono);
  font-size: 10px;
  text-transform: uppercase;
}

.result-badge--success { border-color: #315d4b; color: var(--success); }
.result-badge--warning { border-color: #655331; color: var(--warning); }
.result-badge--error { border-color: #6a3940; color: var(--danger); }

.result-summary {
  min-height: 54px;
  padding: 12px 13px;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
  overflow-wrap: anywhere;
}

.result-summary strong {
  display: block;
  margin-bottom: 2px;
  color: var(--text);
  font-family: var(--mono);
}

.result-messages {
  padding: 10px 13px;
  border-bottom: 1px solid #594828;
  background: #18150f;
  color: #d6bf85;
  font-size: 11px;
}

.result-messages p + p { margin-top: 3px; }

.result-table-wrap {
  max-height: 54vh;
  overflow: auto;
}

.result-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

.result-table th,
.result-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

.result-table th {
  width: 68%;
  color: var(--muted);
  font-weight: 500;
}

.result-table td {
  color: var(--text);
  font-family: var(--mono);
  text-align: right;
}

.result-table td.is-null { color: var(--muted-2); }
.result-table .empty-cell { padding: 18px; color: var(--muted); text-align: center; }

.raw-result {
  border-top: 1px solid var(--border);
}

.raw-result summary {
  padding: 10px 13px;
  color: var(--muted);
  cursor: pointer;
  font-size: 11px;
}

.raw-result pre {
  max-height: 260px;
  margin: 0;
  padding: 12px;
  overflow: auto;
  border-top: 1px solid var(--border);
  background: #090c10;
  color: #aeb9c7;
  font-family: var(--mono);
  font-size: 10px;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

[hidden] { display: none !important; }

@media (max-width: 1120px) {
  .app-shell { grid-template-columns: 272px minmax(0, 1fr); }
  .workspace-grid { grid-template-columns: minmax(0, 1fr); }
  .results-panel { position: static; }
  .field-grid--4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 760px) {
  .topbar { align-items: flex-start; }
  .identity p { display: none; }
  .service-state { font-size: 11px; }
  .safety-notice { align-items: flex-start; flex-direction: column; gap: 2px; }
  .app-shell { display: block; }
  .database-panel { border-right: 0; border-bottom: 1px solid var(--border); }
  .work-area { padding: 12px; }
  .work-toolbar { align-items: flex-start; flex-direction: column; }
  .operation-switch { width: 100%; }
  .operation-button { flex: 1; }
  .selection-strip { grid-template-columns: 1fr; gap: 3px; }
  .workspace-grid { display: block; }
  .results-panel { margin-top: 10px; }
  .field-grid--3,
  .field-grid--4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-actions { align-items: stretch; flex-direction: column; }
  .button--primary { width: 100%; }
}

@media (max-width: 460px) {
  .topbar { padding: 9px 10px; }
  .service-state #health-label { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; }
}
