:root {
  --navy: #14263c;
  --navy-2: #1f3a5c;
  --red: #c8102e;
  --red-dark: #a30d26;
  --bg: #f3f5f8;
  --card: #ffffff;
  --border: #dde3ea;
  --text: #1c2733;
  --muted: #64748b;
  --green: #157f3d;
  --green-bg: #e7f6ec;
  --amber: #92600a;
  --amber-bg: #fdf2dc;
  --red-bg: #fde8ea;
  --blue: #1d5fbf;
  --blue-bg: #e8f0fc;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 8px 24px rgba(16, 24, 40, 0.06);
  font-size: 15px;
}

* { box-sizing: border-box; }

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

.hidden { display: none !important; }
.muted { color: var(--muted); }
.linkish { color: var(--blue); text-decoration: underline; cursor: pointer; }

/* ---------- brand ---------- */
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { margin: 0; font-size: 1.35rem; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 0.9rem; }
.brand-mark {
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; border-radius: 9px;
  background: var(--red); color: #fff; font-weight: 800; font-size: 1.05rem;
  letter-spacing: 0.5px; flex-shrink: 0;
}
.brand-small .brand-mark { width: 34px; height: 34px; font-size: 0.85rem; }
.brand-small div { display: flex; flex-direction: column; line-height: 1.2; }
.brand-small .muted { font-size: 0.82rem; }

/* ---------- login ---------- */
.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(160deg, var(--navy) 0%, var(--navy-2) 60%, #2c4d78 100%);
  padding: 24px;
}
.login-card {
  background: var(--card); border-radius: 14px; box-shadow: var(--shadow);
  padding: 32px; width: 100%; max-width: 400px;
  display: flex; flex-direction: column; gap: 18px;
}

.field { display: flex; flex-direction: column; gap: 6px; }
.field span { font-size: 0.85rem; font-weight: 600; color: var(--navy); }
.field input, .field select {
  font: inherit; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; background: #fff; color: var(--text); width: 100%;
}
.field input:focus, .field select:focus {
  outline: 2px solid rgba(29, 95, 191, 0.35); border-color: var(--blue);
}
.field-hint { font-size: 0.78rem; color: var(--muted); }
#login-terminal { text-transform: uppercase; }

.form-error {
  margin: 0; color: var(--red-dark); background: var(--red-bg);
  border: 1px solid #f5c2c9; border-radius: 8px; padding: 8px 12px; font-size: 0.88rem;
  white-space: pre-wrap;
}

/* ---------- buttons ---------- */
.btn {
  font: inherit; font-weight: 600; border: 1px solid transparent;
  border-radius: 8px; padding: 9px 16px; cursor: pointer; transition: background 0.12s ease;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-block { width: 100%; }
.btn-primary { background: var(--navy); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--navy-2); }
.btn-danger { background: var(--red); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--red-dark); }
.btn-secondary { background: #fff; color: var(--navy); border-color: var(--border); }
.btn-secondary:hover:not(:disabled) { background: #f0f3f7; }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover:not(:disabled) { background: rgba(20, 38, 60, 0.06); color: var(--text); }

/* ---------- topbar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  background: #fff; border-bottom: 1px solid var(--border);
  padding: 12px 24px; position: sticky; top: 0; z-index: 10;
}
.topbar-right { display: flex; align-items: center; gap: 14px; }

.env-switch {
  display: inline-flex; border: 1px solid var(--border); border-radius: 9px;
  overflow: hidden; background: #f0f3f7;
}
.env-btn {
  font: inherit; font-weight: 600; font-size: 0.88rem; border: none;
  background: transparent; color: var(--muted); padding: 7px 16px; cursor: pointer;
}
.env-btn.selected { background: var(--navy); color: #fff; }
.env-btn.env-prod.selected { background: var(--red); }
.env-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.env-btn.env-unconfigured:not(.selected)::after {
  content: "•"; color: var(--amber); margin-left: 5px; font-size: 1rem; line-height: 0;
}
.env-btn.env-unconfigured.selected::after {
  content: "•"; margin-left: 5px; opacity: 0.85; font-size: 1rem; line-height: 0;
}

/* ---------- content ---------- */
.content { max-width: 1280px; margin: 0 auto; padding: 28px 24px 64px; }

.dropzone {
  border: 2px dashed #b9c4d0; border-radius: var(--radius); background: #fff;
  padding: 48px 24px; text-align: center; color: var(--navy);
  cursor: pointer; transition: border-color 0.15s ease, background 0.15s ease;
}
.dropzone:hover, .dropzone:focus, .dropzone.dragover {
  border-color: var(--blue); background: var(--blue-bg); outline: none;
}
.dropzone p { margin: 10px 0 0; }
.dropzone .muted { font-size: 0.85rem; }

/* ---------- table section ---------- */
.table-header {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 16px; margin-bottom: 14px; flex-wrap: wrap;
}
.table-header h2 { margin: 0 0 2px; font-size: 1.15rem; }
.table-actions { display: flex; gap: 10px; }

.progress-wrap { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.progress-bar {
  flex: 1; height: 8px; border-radius: 99px; background: #e2e8f0; overflow: hidden;
}
#progress-fill {
  height: 100%; width: 0%; border-radius: 99px; background: var(--navy);
  transition: width 0.2s ease;
}

.table-scroll {
  background: #fff; border: 1px solid var(--border); border-radius: var(--radius);
  overflow: auto; box-shadow: var(--shadow);
}
.rows-table { border-collapse: collapse; width: 100%; font-size: 0.88rem; }
.rows-table th, .rows-table td {
  padding: 9px 12px; text-align: left; border-bottom: 1px solid #eef1f5;
  white-space: nowrap; vertical-align: top;
}
.rows-table th {
  position: sticky; top: 0; background: #f8fafc; color: var(--muted);
  font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; z-index: 1;
}
.rows-table th.num, .rows-table td.num { text-align: right; }
.rows-table tbody tr:last-child td { border-bottom: none; }
.rows-table tbody tr.row-main:hover { background: #f8fafc; }

.cell-actions { display: flex; gap: 6px; justify-content: flex-end; }
.icon-btn {
  font: inherit; font-size: 0.8rem; font-weight: 600; cursor: pointer;
  border: 1px solid var(--border); background: #fff; color: var(--navy);
  border-radius: 6px; padding: 3px 9px;
}
.icon-btn:hover { background: #f0f3f7; }
.icon-btn.danger { color: var(--red-dark); }
.icon-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* status chips */
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 0.78rem; font-weight: 700; border-radius: 99px; padding: 3px 10px;
  cursor: pointer; border: none; font-family: inherit;
}
.chip-ready { background: var(--blue-bg); color: var(--blue); }
.chip-invalid { background: var(--amber-bg); color: var(--amber); }
.chip-sending { background: #e9e9f9; color: #4c4ca8; }
.chip-sent { background: var(--green-bg); color: var(--green); }
.chip-failed { background: var(--red-bg); color: var(--red-dark); }

.spinner {
  width: 11px; height: 11px; border-radius: 50%;
  border: 2px solid #4c4ca8; border-top-color: transparent;
  animation: spin 0.7s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* detail row */
tr.row-detail td {
  background: #f8fafc; white-space: normal; padding: 12px 16px 16px;
}
.detail-errors { margin: 0 0 8px; padding-left: 18px; color: var(--red-dark); }
.detail-errors li { margin: 2px 0; }
.detail-block { margin-top: 8px; }
.detail-block summary { cursor: pointer; font-weight: 600; color: var(--navy); font-size: 0.83rem; }
.detail-block pre {
  margin: 6px 0 0; background: var(--navy); color: #dbe7f5; border-radius: 8px;
  padding: 12px; font-size: 0.78rem; overflow: auto; max-height: 320px;
}
.detail-message { margin: 0 0 4px; font-size: 0.86rem; }

/* ---------- preview slideout ---------- */
.drawer-overlay {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.45); z-index: 40;
  animation: fade 0.15s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 50;
  width: min(560px, 100vw); background: #fff; box-shadow: -8px 0 30px rgba(16, 24, 40, 0.18);
  display: flex; flex-direction: column; transform: translateX(0);
  transition: transform 0.22s ease;
}
.drawer.hidden { display: flex !important; transform: translateX(102%); pointer-events: none; }
.drawer-head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  padding: 18px 20px; border-bottom: 1px solid var(--border);
}
.drawer-head h3 { margin: 0 0 2px; font-size: 1.1rem; }
.drawer-head .muted { font-size: 0.85rem; }
.drawer-body { flex: 1; overflow: auto; padding: 18px 20px; }
.drawer-foot {
  display: flex; justify-content: flex-end; gap: 10px;
  padding: 14px 20px; border-top: 1px solid var(--border); background: #f8fafc;
}
.preview-reqline { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.method-pill {
  background: var(--green); color: #fff; font-weight: 700; font-size: 0.75rem;
  letter-spacing: 0.05em; border-radius: 6px; padding: 3px 9px;
}
.preview-reqline code {
  font-size: 0.82rem; color: var(--navy); word-break: break-all; background: #f0f3f7;
  border-radius: 6px; padding: 4px 8px;
}
.preview-label { margin: 16px 0 6px; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.drawer-body pre {
  margin: 0; background: var(--navy); color: #dbe7f5; border-radius: 8px;
  padding: 14px; font-size: 0.8rem; line-height: 1.5; overflow: auto;
}
.preview-warning {
  margin: 0 0 12px; color: var(--amber); background: var(--amber-bg);
  border: 1px solid #f0dca6; border-radius: 8px; padding: 8px 12px; font-size: 0.85rem;
}

/* ---------- dialogs ---------- */
dialog {
  border: none; border-radius: 14px; box-shadow: var(--shadow);
  padding: 0; width: min(720px, calc(100vw - 48px));
}
dialog::backdrop { background: rgba(15, 23, 42, 0.45); }
#edit-form, .confirm-body { padding: 24px; }
#edit-form h3, .confirm-body h3 { margin: 0 0 16px; }
.edit-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px 14px;
}
.edit-grid .field.span-2 { grid-column: span 2; }
.edit-grid .field.span-3 { grid-column: span 3; }
.field .field-err { color: var(--red-dark); font-size: 0.78rem; font-weight: 500; }
.field input.invalid, .field select.invalid { border-color: var(--red); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }
#confirm-dialog { width: min(440px, calc(100vw - 48px)); }

@media (max-width: 720px) {
  .edit-grid { grid-template-columns: 1fr 1fr; }
  .edit-grid .field.span-3 { grid-column: span 2; }
}
