:root {
  color-scheme: light dark;

  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --border: #e1e4e9;
  --border-strong: #c9cdd4;
  --text: #14161a;
  --text-muted: #6b7280;
  --text-faint: #9aa1ab;

  --accent: #3b5bfd;
  --accent-hover: #2e49e0;
  --accent-contrast: #ffffff;
  --accent-soft: #eef1ff;

  --success: #1a7f4e;
  --success-bg: #e7f8ef;
  --success-border: #b6ecd0;
  --danger: #b3261e;
  --danger-bg: #fdecea;
  --danger-border: #f6c5c0;
  --warning: #8a5a00;
  --warning-bg: #fff6e0;
  --warning-border: #f5dfa0;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 4px 12px rgba(16, 24, 40, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0c0d10;
    --surface: #17181c;
    --surface-2: #1d1f24;
    --border: #2a2d34;
    --border-strong: #3a3e47;
    --text: #f1f2f4;
    --text-muted: #9aa1ab;
    --text-faint: #6b7280;

    --accent: #6d84ff;
    --accent-hover: #8296ff;
    --accent-contrast: #0c0d10;
    --accent-soft: #1b2040;

    --success: #4ade9a;
    --success-bg: #10261c;
    --success-border: #1e4432;
    --danger: #ff8a80;
    --danger-bg: #2a1615;
    --danger-border: #4a2422;
    --warning: #f2c14e;
    --warning-bg: #2a2210;
    --warning-border: #4a3c1a;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  background: var(--bg);
  color: var(--text);
  margin: 0;
  padding: 1.5rem clamp(1rem, 4vw, 2.5rem) 4rem;
}

.app {
  max-width: 1100px;
  margin: 0 auto;
}

/* Header */

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.75rem;
  padding-bottom: 1.25rem;
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: baseline; gap: 0.6rem; flex-wrap: wrap; }
h1 { font-size: 1.3rem; font-weight: 700; letter-spacing: -0.01em; margin: 0; }
.version {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-faint);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.1rem 0.55rem;
}

/* Tabs */

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  margin-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}
.tab {
  background: transparent;
  border: none;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  color: var(--text-muted);
  font-weight: 600;
  padding: 0.6rem 1rem;
  margin-bottom: -1px;
  border-bottom: 2px solid transparent;
}
.tab:hover:not(:disabled):not(.active) { background: var(--surface-2); color: var(--text); border-color: transparent; }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

.create-panel {
  padding: 0;
  overflow: hidden;
}
#create-iframe {
  display: block;
  width: 100%;
  height: calc(100vh - 220px);
  min-height: 640px;
  border: none;
}

/* Cards */

section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 1.25rem 1.35rem;
  margin-bottom: 1.25rem;
}

section > label:first-child,
section > .card-title {
  display: block;
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 0.7rem;
}

.field { margin-bottom: 0.9rem; }
.field:last-of-type { margin-bottom: 0; }
.field label { display: block; font-size: 0.85rem; font-weight: 600; margin-bottom: 0.35rem; color: var(--text); }

input[type="text"],
input[type="password"],
textarea {
  width: 100%;
  font: inherit;
  font-size: 0.9rem;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.55rem 0.7rem;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input[type="text"],
input[type="password"] { max-width: 420px; }

input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

textarea { resize: vertical; line-height: 1.5; }

.hint { font-size: 0.8rem; color: var(--text-muted); line-height: 1.5; margin: 0.6rem 0 0; }

/* Buttons */

.actions { margin-top: 1rem; display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.actions.actions-top { margin-top: 0; }

button {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  padding: 0.55rem 1.1rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-contrast);
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.05s ease;
}
button:hover:not(:disabled):not(.tab) { background: var(--accent-hover); border-color: var(--accent-hover); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.55; cursor: not-allowed; }
button:focus { outline: none; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

button.secondary {
  background: transparent;
  color: var(--text);
  border-color: var(--border-strong);
}
button.secondary:hover:not(:disabled) { background: var(--surface-2); border-color: var(--border-strong); }

button.is-loading { position: relative; color: transparent; }
button.is-loading::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.4);
  border-top-color: var(--accent-contrast);
  animation: spin 0.7s linear infinite;
}
button.secondary.is-loading::after { border: 2px solid var(--border-strong); border-top-color: var(--text); }
@keyframes spin { to { transform: rotate(360deg); } }

/* Status text */

.status { font-size: 0.85rem; font-weight: 500; color: var(--text-muted); }
.status.status-success { color: var(--success); }
.status.status-error { color: var(--danger); }

/* Table */

#table-wrap {
  overflow-x: auto;
  margin-top: 1.1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

table { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 0.83rem; }

th, td {
  padding: 0.55rem 0.7rem;
  text-align: left;
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}

th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface-2);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border-strong);
}

tbody tr:nth-child(even) { background: var(--surface-2); }
tbody tr:hover { background: var(--accent-soft); }
tbody tr:last-child td { border-bottom: none; }

/* Keep the serial number visible while scrolling a wide table sideways */
th:first-child, td:first-child {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--surface);
  box-shadow: 1px 0 0 var(--border);
}
th:first-child { z-index: 3; background: var(--surface-2); }
tbody tr:nth-child(even) td:first-child { background: var(--surface-2); }
tbody tr:hover td:first-child { background: var(--accent-soft); }

td input, td select {
  width: 100%;
  min-width: 100px;
  max-width: 230px;
  border: 1px solid transparent;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 0.83rem;
  border-radius: var(--radius-sm);
  padding: 0.3rem 0.4rem;
  transition: border-color 0.15s ease, background 0.15s ease;
}
td input:hover, td select:hover { border-color: var(--border); }
td input:focus, td select:focus { border-color: var(--accent); background: var(--surface); box-shadow: 0 0 0 3px var(--accent-soft); }
td select { background: var(--surface-2); border-color: var(--border); }

td.editable { position: relative; }
td.editable.dirty { background: var(--warning-bg); }
td.editable.dirty::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--warning);
}
td.editable.dirty select,
td.editable.dirty input { background: var(--surface); }

tr.row-error td { background: var(--danger-bg); }
tr.row-saved td { background: var(--success-bg); }
tr.row-error:hover td, tr.row-saved:hover td, td.editable.dirty:hover { filter: brightness(0.98); }

.row-note { font-size: 0.78rem; color: var(--text-muted); white-space: normal; min-width: 160px; }

.eol-cell { display: flex; align-items: center; gap: 0.35rem; flex-wrap: wrap; }
.eol-cell input[type="date"] { min-width: 130px; max-width: 150px; }
button.small { padding: 0.3rem 0.55rem; font-size: 0.75rem; flex-shrink: 0; }
.field-note { font-size: 0.72rem; color: var(--text-muted); white-space: normal; margin-top: 0.2rem; max-width: 200px; }
.field-note-error { color: var(--danger); }

/* Status badges (not_found / multiple / error) */
.badge {
  display: inline-flex;
  align-items: center;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  margin-right: 0.5rem;
  border: 1px solid transparent;
}
.badge-error { color: var(--danger); background: var(--danger-bg); border-color: var(--danger-border); }
.badge-warning { color: var(--warning); background: var(--warning-bg); border-color: var(--warning-border); }
.badge-success { color: var(--success); background: var(--success-bg); border-color: var(--success-border); }

@media (max-width: 480px) {
  section { padding: 1rem; border-radius: var(--radius-md); }
  h1 { font-size: 1.1rem; }
}
