:root {
  --bg: #f5f8fa;
  --surface: #ffffff;
  --surface-2: #eef5fa;
  --text: #23313a;
  --muted: #64737a;
  --line: #d7e1e7;
  --primary: #006298;
  --primary-dark: #004b73;
  --accent: #b86626;
  --danger: #b3261e;
  --shadow: 0 18px 50px rgba(35, 49, 58, 0.12);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-width: 0;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  align-items: center;
  border: 0;
  border-radius: 6px;
  background: var(--primary);
  color: white;
  cursor: pointer;
  display: inline-flex;
  font-weight: 700;
  justify-content: center;
  min-height: 42px;
  padding: 0 16px;
}

button:hover {
  background: var(--primary-dark);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

button.secondary {
  background: #e6edf2;
  color: var(--text);
}

button.secondary:hover {
  background: #d6e3eb;
}

button.danger {
  color: var(--danger);
}

button.text-button {
  background: transparent;
  color: var(--primary);
  min-height: auto;
  padding: 0;
  text-align: left;
}

button.text-button:hover {
  background: transparent;
  color: var(--primary-dark);
  text-decoration: underline;
}

input,
select,
textarea {
  width: 100%;
  max-width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
  color: var(--text);
  padding: 8px 10px;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(0, 98, 152, 0.16);
  outline: 0;
}

textarea {
  line-height: 1.45;
  min-height: 220px;
  resize: vertical;
}

code {
  background: #e6edf2;
  border-radius: 4px;
  color: var(--primary-dark);
  display: inline-block;
  font-size: 0.82rem;
  margin: 2px;
  padding: 2px 5px;
}

label,
fieldset {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
}

fieldset {
  border: 0;
  margin: 0;
  padding: 0;
}

legend {
  color: var(--muted);
  font-weight: 800;
  margin-bottom: 8px;
}

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

h1 {
  font-size: clamp(1.4rem, 2vw, 2rem);
}

h2 {
  font-size: 1.25rem;
}

h3 {
  font-size: 1rem;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 10px;
  text-align: left;
  vertical-align: top;
}

th {
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
}

td span {
  color: var(--muted);
  font-size: 0.82rem;
}

.auth-wrap {
  align-items: center;
  display: grid;
  min-height: 100vh;
  padding: 24px;
}

.auth-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 20px;
  margin: 0 auto;
  max-width: 440px;
  padding: 28px;
  width: 100%;
}

.mfa-setup-box {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  display: grid;
  gap: 12px;
  padding: 12px;
}

.mfa-setup-box strong {
  color: var(--primary);
  font-size: 1.05rem;
  overflow-wrap: anywhere;
}

.mfa-qr-wrap {
  align-items: center;
  background: white;
  border: 1px solid var(--line);
  border-radius: 6px;
  display: grid;
  justify-items: center;
  padding: 12px;
}

.mfa-qr-code {
  aspect-ratio: 1;
  display: block;
  height: auto;
  max-width: min(220px, 100%);
  width: 220px;
}

.mfa-key-label {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
  text-transform: uppercase;
}

.modal-backdrop {
  align-items: center;
  background: rgba(35, 49, 58, 0.45);
  display: grid;
  inset: 0;
  justify-items: center;
  padding: 18px;
  position: fixed;
  z-index: 20;
}

.modal-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 16px;
  max-width: 440px;
  padding: 20px;
  width: min(440px, 100%);
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.auth-brand,
.app-brand {
  align-items: flex-start;
  display: grid;
  gap: 10px;
  min-width: 0;
}

.brand-logo {
  display: block;
  height: auto;
  max-width: min(260px, 100%);
}

.app-brand .brand-logo {
  max-width: min(240px, 52vw);
}

.topbar {
  align-items: center;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 18px clamp(16px, 4vw, 40px);
}

.top-actions {
  align-items: center;
  display: flex;
  gap: 12px;
}

.user-summary {
  display: grid;
  gap: 3px;
  min-width: 0;
  text-align: right;
}

.user-summary small,
.muted-copy {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.tabs {
  background: #edf3f7;
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 10px clamp(16px, 4vw, 40px);
}

.tab {
  align-items: center;
  appearance: none;
  background: transparent;
  border: 0;
  border-radius: 6px;
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  font: inherit;
  font-weight: 700;
  justify-content: center;
  line-height: 1;
  margin: 0;
  min-height: 36px;
  padding: 0 16px;
  text-decoration: none;
  white-space: nowrap;
}

.tab.active {
  background: var(--surface);
  color: var(--text);
}

.tab:hover {
  background: rgba(0, 98, 152, 0.08);
  color: var(--text);
}

.tab.active:hover {
  background: var(--surface);
}

.workspace {
  padding: clamp(16px, 4vw, 40px);
}

.calc-grid {
  align-items: start;
  display: grid;
  gap: 20px;
  grid-template-columns: minmax(280px, 0.9fr) minmax(0, 1.1fr);
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 26px rgba(35, 49, 58, 0.06);
  min-width: 0;
  padding: 20px;
}

.calculator-panel {
  align-content: start;
  display: grid;
  gap: 18px;
}

.field-row,
.settings-grid {
  display: grid;
  gap: 14px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.email-delivery-editor {
  display: grid;
  gap: 16px;
}

.checkbox-label {
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  display: flex;
  gap: 10px;
  min-height: 40px;
  padding: 8px 10px;
}

.checkbox-label input[type="checkbox"] {
  flex: 0 0 auto;
  min-height: auto;
  width: 16px;
}

.readonly-field {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  display: grid;
  gap: 5px;
  padding: 10px 12px;
}

.readonly-field span {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.readonly-field strong {
  color: var(--text);
}

.setup-notes {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--muted);
  display: grid;
  gap: 12px;
  padding: 12px;
}

.setup-notes h3 {
  color: var(--text);
}

.setup-notes strong {
  color: var(--primary-dark);
}

.setup-notes ul {
  margin: 6px 0 0;
  padding-left: 18px;
}

.setup-notes li {
  margin: 4px 0;
}

.input-unit {
  align-items: center;
  background: white;
  border: 1px solid var(--line);
  border-radius: 6px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  overflow: hidden;
}

.input-unit input {
  border: 0;
  border-radius: 0;
  min-width: 0;
}

.input-unit span {
  color: var(--muted);
  padding: 0 10px;
}

.choice-grid {
  display: grid;
  gap: 10px;
}

.choice {
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  display: grid;
  font-weight: 600;
  grid-template-columns: 18px minmax(0, 1fr);
  min-height: 48px;
  padding: 10px 12px;
}

.choice span {
  min-width: 0;
  overflow-wrap: anywhere;
}

.choice input[type="radio"] {
  min-height: auto;
  width: 16px;
}

.choice strong {
  color: var(--primary);
  margin-left: 6px;
}

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

.empty-result {
  align-content: center;
  color: var(--muted);
  display: grid;
  gap: 10px;
  min-height: 320px;
  text-align: center;
}

.result-header {
  border-bottom: 1px solid var(--line);
  padding-bottom: 16px;
}

.result-header h2 {
  font-size: clamp(1.65rem, 4vw, 2.5rem);
}

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

.metric {
  background: #f4f7f9;
  border-left: 4px solid var(--primary);
  border-radius: 6px;
  display: grid;
  gap: 6px;
  padding: 14px;
}

.metric.accent {
  border-left-color: var(--accent);
}

.metric span {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.metric strong {
  font-size: 1.4rem;
  overflow-wrap: anywhere;
}

.breakdown {
  display: grid;
  gap: 10px;
}

.result-actions {
  border-top: 1px solid var(--line);
  padding-top: 16px;
}

dl {
  display: grid;
  gap: 8px;
  margin: 0;
}

dl div {
  display: flex;
  justify-content: space-between;
  gap: 16px;
}

dt {
  color: var(--muted);
  font-weight: 700;
}

dd {
  margin: 0;
  text-align: right;
}

.items-table-wrap,
.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.table-scroll.tall {
  max-height: 560px;
}

.admin-grid {
  display: grid;
  gap: 20px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.admin-grid .wide {
  grid-column: 1 / -1;
}

.admin-alert {
  margin-bottom: 16px;
}

.panel-heading {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.panel-heading h2 {
  min-width: 0;
}

.panel-heading button {
  flex: 0 0 auto;
}

.compact-label {
  max-width: 420px;
  margin-bottom: 16px;
}

.editor-toolbar {
  align-items: end;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}

.editor-toolbar .compact-label {
  margin-bottom: 0;
  min-width: min(420px, 100%);
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.template-editor {
  display: grid;
  gap: 10px;
  margin-top: 16px;
}

.template-editor-heading {
  align-items: baseline;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: space-between;
}

.template-editor-heading span:first-child {
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 800;
}

.template-editor-heading span:last-child {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}

.template-editor-actions {
  align-items: center;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px;
}

.template-editor-actions button {
  min-height: 36px;
  padding: 0 10px;
}

.template-editor-actions select {
  flex: 0 1 190px;
  min-height: 36px;
  width: auto;
}

.wysiwyg-surface,
.wysiwyg-html-source {
  background: white;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  line-height: 1.5;
  min-height: 420px;
  overflow: auto;
  padding: 18px;
  width: 100%;
}

.template-editor .tox-tinymce {
  border-color: var(--line);
  border-radius: 6px;
  font-family: inherit;
  width: 100%;
}

.template-editor .tox .tox-editor-header {
  background: var(--surface-2);
  border-bottom-color: var(--line);
}

.template-editor .tox .tox-toolbar-overlord,
.template-editor .tox .tox-toolbar,
.template-editor .tox .tox-toolbar__overflow,
.template-editor .tox .tox-toolbar__primary {
  background-color: var(--surface-2);
}

.template-editor .tox .tox-edit-area::before {
  border-color: var(--primary);
}

.wysiwyg-html-source:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(0, 98, 152, 0.16);
  outline: 0;
}

.wysiwyg-html-source {
  font-family: "Cascadia Code", "SFMono-Regular", Consolas, monospace;
  font-size: 0.86rem;
  resize: vertical;
  white-space: pre;
}

.template-fallback {
  margin-top: 8px;
}

.template-fallback textarea {
  min-height: 130px;
}

.admin-grid table input,
.admin-grid table select {
  min-width: 120px;
}

.table-actions {
  white-space: nowrap;
}

.user-form {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  margin-bottom: 14px;
}

.user-form button {
  grid-column: 1 / -1;
}

.eyebrow {
  color: var(--primary);
  font-size: 0.76rem;
  font-weight: 900;
  text-transform: uppercase;
}

.alert {
  background: #fff0ed;
  border: 1px solid #efc0ba;
  border-radius: 6px;
  color: var(--danger);
  padding: 10px 12px;
}

.notice {
  background: #edf7fc;
  border: 1px solid #b7d8e8;
  border-radius: 6px;
  color: var(--primary-dark);
  padding: 10px 12px;
}

@media (max-width: 920px) {
  .calc-grid,
  .admin-grid,
  .metric-grid,
  .field-row,
  .settings-grid {
    grid-template-columns: 1fr;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .top-actions {
    width: 100%;
    justify-content: space-between;
  }

  .user-summary {
    text-align: left;
  }
}

@media (max-width: 700px) {
  .auth-wrap {
    align-items: start;
    min-height: 100dvh;
    padding: 14px;
  }

  .auth-panel,
  .panel {
    border-radius: 6px;
    box-shadow: none;
    padding: 16px;
  }

  .auth-panel {
    gap: 16px;
    margin-top: 18px;
  }

  .brand-logo {
    max-width: min(230px, 100%);
  }

  .app-brand .brand-logo {
    max-width: min(220px, 72vw);
  }

  .topbar {
    gap: 14px;
    padding: 14px;
  }

  .top-actions {
    align-items: stretch;
    flex-direction: column;
    gap: 10px;
  }

  .top-actions button {
    width: 100%;
  }

  .tabs {
    gap: 8px;
    padding: 8px 14px;
  }

  .tab {
    flex: 1 0 120px;
  }

  .workspace {
    padding: 14px;
  }

  .calc-grid {
    gap: 14px;
    grid-template-columns: minmax(0, 1fr);
  }

  .calculator-panel,
  .result-panel {
    gap: 16px;
  }

  .calculator-panel > button,
  .auth-panel > button {
    width: 100%;
  }

  .choice {
    align-items: start;
    min-height: 44px;
  }

  .result-header h2 {
    font-size: 1.6rem;
  }

  .metric strong {
    font-size: 1.25rem;
  }

  dl div {
    display: grid;
    gap: 4px;
  }

  dd {
    text-align: left;
  }

  .panel-heading {
    align-items: stretch;
    flex-direction: column;
    gap: 10px;
  }

  .panel-heading button,
  .editor-toolbar button,
  .action-row button,
  .user-form button,
  .result-actions button,
  .modal-actions button {
    width: 100%;
  }

  .editor-toolbar,
  .action-row {
    align-items: stretch;
    flex-direction: column;
  }

  .template-editor-actions {
    align-items: stretch;
  }

  .template-editor-actions select {
    flex: 1 1 100%;
    width: 100%;
  }

  .template-editor-actions button {
    flex: 1 1 calc(50% - 8px);
  }

  .wysiwyg-surface,
  .wysiwyg-html-source {
    min-height: 340px;
  }

  .template-editor .tox-tinymce {
    min-height: 340px;
  }

  .admin-grid {
    gap: 14px;
  }

  .admin-grid table {
    min-width: 640px;
  }

  .table-actions button {
    width: auto;
  }

  .user-form {
    grid-template-columns: 1fr;
  }

  .modal-actions {
    flex-direction: column;
  }
}

@media (max-width: 420px) {
  h1 {
    font-size: 1.3rem;
  }

  .metric {
    padding: 12px;
  }

  th,
  td {
    padding: 8px;
  }
}
