:root {
  color-scheme: light;
  --bg: #f5f6f2;
  --panel: #ffffff;
  --ink: #1e2528;
  --muted: #647074;
  --line: #ccd5d5;
  --accent: #0b6b63;
  --warn: #9a5b00;
  --error: #a82a2a;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.shell {
  width: min(1320px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 24px 0 40px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  padding-bottom: 16px;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
}

h2 {
  font-size: 18px;
  margin-bottom: 16px;
}

h3 {
  color: var(--muted);
  font-size: 14px;
  margin: 18px 0 6px;
}

.form-section:first-of-type h3 {
  margin-top: 0;
}

.topbar p {
  color: var(--muted);
  margin-top: 4px;
}

.notice {
  margin: 16px 0 0;
  border-left: 5px solid #d9a300;
  background: #fff8d8;
  padding: 12px 16px;
  font-weight: 700;
}

.legend {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 8px;
  margin-top: 16px;
}

.legend div {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 9px 10px;
}

.legend small {
  display: block;
  color: var(--muted);
  margin-top: 3px;
}

.swatch {
  display: inline-block;
  width: 18px;
  height: 12px;
  border: 1px solid #aeb9bc;
  border-radius: 3px;
  margin-right: 7px;
  vertical-align: -1px;
}

.sw-normal { background: #fff; }
.sw-regional { background: #fff8cf; }
.sw-auto { background: #f0f2f5; }
.sw-rare { background: #fff0f0; }
.sw-pdf { background: #f0f8ff; }
.sw-error,
.sw-ng { background: #fdeaea; }

.status {
  min-width: 112px;
  text-align: center;
  border: 1px solid var(--line);
  background: #fff;
  padding: 8px 12px;
  border-radius: 6px;
  font-weight: 700;
}

.status-dirty,
.status-warn {
  color: var(--warn);
  border-color: #d2a354;
  background: #fff7e6;
}

.status-busy {
  color: #215b7a;
  border-color: #84adc2;
  background: #edf7fb;
}

.status-done {
  color: var(--accent);
  border-color: #6db5aa;
  background: #eef8f5;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-end;
  padding: 16px 0;
}

button {
  min-height: 40px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  padding: 0 18px;
  font-weight: 800;
  cursor: pointer;
}

.action-buttons {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
}

.secondary-button {
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
}

.toolbar-label {
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 6px;
}

.preset-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.preset-button {
  min-height: 34px;
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
  padding: 0 12px;
  font-weight: 700;
}

.preset-button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.preset-load-error {
  color: var(--error);
}

button:disabled {
  opacity: 0.55;
  cursor: wait;
}

.main-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 16px;
}

.main-tab {
  min-height: 38px;
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
  padding: 0 14px;
  font-weight: 800;
}

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

.main-panel {
  display: none;
}

.main-panel.active {
  display: block;
}

.input-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
  gap: 14px;
  align-items: start;
}

.input-grid > .form-section {
  display: contents;
}

.pile-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
  gap: 14px;
  align-items: start;
}

.input-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  margin-bottom: 0;
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(300px, 400px);
  gap: 16px;
  align-items: start;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
}

.form-section {
  display: block;
}

.field-row {
  display: grid;
  grid-template-columns: minmax(190px, 1fr) minmax(170px, 260px);
  gap: 10px;
  align-items: center;
  min-height: 52px;
  border-bottom: 1px solid #edf0f0;
  position: relative;
  padding: 8px 8px;
  border-radius: 6px;
}

.field-row:last-child {
  border-bottom: 0;
}

.field-row::after {
  content: attr(data-badge);
  display: none;
  position: absolute;
  right: 12px;
  top: -7px;
  max-width: min(260px, calc(100% - 24px));
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: 1px solid #9ac7ea;
  border-radius: 999px;
  background: #eaf3ff;
  color: #1b5f91;
  padding: 1px 7px;
  font-size: 11px;
  font-weight: 700;
}

.result-panel {
  position: sticky;
  top: 12px;
}

.result-wide-panel {
  max-width: none;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px;
  margin-bottom: 16px;
}

.kpi-card {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 9px 10px;
}

.kpi-card span,
.kpi-card small {
  display: block;
  color: var(--muted);
}

.kpi-card b {
  display: block;
  margin: 3px 0;
  font-size: 16px;
}

.reason {
  min-width: 220px;
}

.source-note {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
  margin-bottom: 12px;
}

.status-table {
  border-collapse: collapse;
  width: 100%;
  font-size: 13px;
}

.status-table th,
.status-table td {
  border: 1px solid var(--line);
  padding: 8px 10px;
  vertical-align: top;
}

.status-table th {
  background: #e8f0f2;
  text-align: left;
}

.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.file-button {
  display: inline-grid;
  place-items: center;
  min-height: 40px;
  border-radius: 6px;
  padding: 0 14px;
  font-weight: 800;
  cursor: pointer;
}

.group-status {
  display: none;
}

.field-row[data-badge]::after {
  display: block;
}

.field-text b {
  display: block;
}

.field-text small {
  display: block;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.35;
  margin-top: 3px;
}

.field-control {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-items: center;
}

.field-control textarea {
  grid-column: 1 / -1;
}

.unit {
  display: none;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.field-row.field-auto {
  background: #f0f2f5;
}

.field-row.field-regional {
  background: #fff8cf;
}

.field-row.field-rare {
  background: #fff0f0;
}

.field-row.field-pdf {
  background: #f8fbff;
}

.field-row.field-auto-zero {
  border: 1px solid #9ac7ea;
  background: #f0f8ff;
}

.field-row.field-error {
  border: 2px solid var(--error);
  background: #fff0f0;
}

.field-row.field-warn {
  border: 2px solid #d2a354;
  background: #fff7e6;
}

.field-row.field-calc-ng {
  border: 2px solid var(--error);
  background: #fdeaea;
}

input,
select,
textarea {
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 8px;
  font: inherit;
  width: 100%;
  background: #fff;
  min-width: 0;
}

textarea {
  resize: vertical;
}

dl {
  margin: 0;
}

dl div {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid #edf0f0;
}

dt {
  color: var(--muted);
}

dd {
  margin: 0;
  font-weight: 800;
}

.selected-preset {
  color: var(--muted);
  border-bottom: 1px solid #edf0f0;
  padding-bottom: 10px;
  margin-bottom: 4px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 14px;
}

.summary-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  min-height: 38px;
}

.ok-chip {
  color: var(--accent);
}

.ng-chip {
  color: var(--error);
}

.neutral-chip {
  color: var(--muted);
}

.messages {
  margin-top: 14px;
}

.messages p {
  padding: 10px;
  border-radius: 6px;
  margin-top: 8px;
}

.warn {
  color: var(--warn);
  background: #fff7e6;
}

.error {
  color: var(--error);
  background: #fff0f0;
}

@media (max-width: 1040px) {
  .topbar,
  .layout,
  .toolbar {
    grid-template-columns: 1fr;
    display: grid;
  }

  .action-buttons {
    justify-content: stretch;
  }

  .action-buttons button {
    flex: 1;
  }

  .summary-grid {
    grid-template-columns: 1fr;
  }

  .input-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .pile-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .field-row {
    grid-template-columns: 1fr;
    padding: 10px 8px;
  }

  .field-row::after {
    position: static;
    justify-self: start;
    max-width: 100%;
    margin-top: 4px;
  }
}

@media (max-width: 760px) {
  .main-tabs {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .action-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .action-buttons button,
  .file-button {
    width: 100%;
  }

  .input-grid,
  .pile-grid {
    grid-template-columns: 1fr;
  }
}

.login-shell {
  max-width: 720px;
  margin: 48px auto;
}

.login-panel {
  max-width: 420px;
  margin: 0 auto;
  padding: 28px;
}

.login-panel h1 {
  margin: 0 0 8px;
  font-size: 24px;
}

.login-version {
  margin: 0 0 16px;
  color: var(--muted);
}

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

.login-form label {
  display: grid;
  gap: 8px;
}

.login-form input {
  width: 100%;
}

.warning-message,
.error-message {
  margin: 12px 0 0;
  padding: 12px 14px;
  border-radius: 8px;
}

.warning-message {
  background: #fff6d8;
  border: 1px solid #e0c77c;
}

.error-message {
  background: #fbe4e4;
  border: 1px solid #dc8e8e;
}

.logout-form {
  display: flex;
  align-items: center;
}
