:root {
  color-scheme: dark;
  /* Catppuccin Macchiato palette: https://catppuccin.com/palette */
  --ctp-rosewater: #f4dbd6;
  --ctp-flamingo: #f0c6c6;
  --ctp-pink: #f5bde6;
  --ctp-mauve: #c6a0f6;
  --ctp-red: #ed8796;
  --ctp-maroon: #ee99a0;
  --ctp-peach: #f5a97f;
  --ctp-yellow: #eed49f;
  --ctp-green: #a6da95;
  --ctp-teal: #8bd5ca;
  --ctp-sky: #91d7e3;
  --ctp-sapphire: #7dc4e4;
  --ctp-blue: #8aadf4;
  --ctp-lavender: #b7bdf8;
  --ctp-text: #cad3f5;
  --ctp-subtext1: #b8c0e0;
  --ctp-subtext0: #a5adcb;
  --ctp-overlay2: #939ab7;
  --ctp-overlay1: #8087a2;
  --ctp-overlay0: #6e738d;
  --ctp-surface2: #5b6078;
  --ctp-surface1: #494d64;
  --ctp-surface0: #363a4f;
  --ctp-base: #24273a;
  --ctp-mantle: #1e2030;
  --ctp-crust: #181926;
  --bg: var(--ctp-base);
  --panel: rgba(36, 39, 58, 0.94);
  --panel-strong: #2b3045;
  --line: var(--ctp-surface0);
  --line-strong: var(--ctp-surface1);
  --text: var(--ctp-text);
  --muted: var(--ctp-subtext0);
  --accent: var(--ctp-mauve);
  --accent-strong: var(--ctp-blue);
  --shadow: 0 24px 60px rgba(76, 79, 105, 0.14);
  font-family: "Yu Gothic", "Hiragino Sans", "Meiryo", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--text);
  background: var(--ctp-mantle);
}

.page {
  width: min(1380px, calc(100% - 24px));
  margin: 0 auto;
  padding: 16px 0 20px;
}

.hero {
  margin-bottom: 12px;
}

.hero h1 {
  margin: 0;
  font-size: clamp(1.7rem, 3vw, 2.35rem);
  line-height: 1.1;
}

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

.panel {
  border: 1px solid rgba(54, 58, 79, 0.95);
  border-radius: 18px;
  padding: 14px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.panel-heading {
  margin-bottom: 10px;
}

.panel-heading h2 {
  margin: 0;
  font-size: 1rem;
}

.panel-heading p {
  margin: 4px 0 0;
  color: var(--muted);
  line-height: 1.4;
  font-size: 0.88rem;
}

.saved-inputs {
  margin-bottom: 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel-strong);
}

.saved-inputs summary {
  list-style: none;
  padding: 10px 12px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 700;
}

.saved-inputs summary::-webkit-details-marker {
  display: none;
}

.saved-inputs summary::after {
  content: "+";
  float: right;
  color: var(--muted);
  font-size: 1rem;
}

.saved-inputs[open] summary::after {
  content: "-";
}

.saved-inputs-body {
  padding: 0 10px 10px;
}

.saved-inputs-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: end;
  margin-top: 10px;
}

.saved-input-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.form-section {
  margin: 0 0 10px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel-strong);
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px 10px;
}

.form-section legend {
  padding: 0 6px;
  font-weight: 700;
  font-size: 0.92rem;
}

.field {
  display: grid;
  gap: 4px;
  margin: 0;
}

.field span {
  font-size: 0.84rem;
  font-weight: 600;
}

.field small {
  color: var(--muted);
  line-height: 1.3;
  font-size: 0.74rem;
}

input {
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  padding: 8px 10px;
  color: var(--text);
  background: rgba(24, 25, 38, 0.9);
  font: inherit;
  min-height: 38px;
}

select {
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  padding: 8px 10px;
  color: var(--text);
  background: rgba(24, 25, 38, 0.9);
  font: inherit;
  min-height: 38px;
}

input:focus {
  outline: 2px solid rgba(183, 189, 248, 0.26);
  outline-offset: 1px;
  border-color: var(--ctp-lavender);
}

select:focus {
  outline: 2px solid rgba(183, 189, 248, 0.26);
  outline-offset: 1px;
  border-color: var(--ctp-lavender);
}

.action-button {
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  padding: 0 14px;
  color: var(--text);
  background: rgba(36, 39, 58, 0.9);
  font: inherit;
  font-weight: 700;
  min-height: 38px;
  cursor: pointer;
  transition: transform 120ms ease, border-color 120ms ease, background 120ms ease;
}

.action-button:hover {
  border-color: var(--ctp-lavender);
  background: rgba(54, 58, 79, 0.96);
}

.action-button:focus {
  outline: 2px solid rgba(183, 189, 248, 0.26);
  outline-offset: 1px;
}

.action-button:active {
  transform: translateY(1px);
}

.action-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.action-button-primary {
  border-color: rgba(138, 173, 244, 0.72);
  background: rgba(138, 173, 244, 0.16);
}

.action-button-danger {
  border-color: rgba(237, 135, 150, 0.72);
  background: rgba(237, 135, 150, 0.12);
}

.ratio-inputs {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  gap: 6px;
  align-items: center;
}

.ratio-separator {
  font-size: 1rem;
  font-weight: 700;
  color: var(--ctp-sapphire);
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.status-message {
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
}

.saved-inputs .status-message {
  margin-top: 8px;
}

.results-table-wrapper {
  overflow-x: auto;
}

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

.results-table th,
.results-table td {
  padding: 7px 8px;
  border-bottom: 1px solid rgba(73, 77, 100, 0.85);
  text-align: left;
}

.results-table th {
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 700;
}

.results-table td:first-child {
  font-weight: 700;
  width: 34%;
  white-space: nowrap;
}

.metric-label-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.metric-subtext {
  display: block;
  margin-top: 1px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 500;
}

.metric-value {
  display: inline-block;
  min-width: 4.5ch;
}

.metric-value-warning {
  color: var(--ctp-yellow);
  font-weight: 700;
}

.metric-value-danger {
  color: var(--ctp-red);
  font-weight: 700;
}

.metric-help-button {
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  border: 1px solid rgba(145, 215, 227, 0.55);
  border-radius: 999px;
  padding: 0;
  color: var(--ctp-sky);
  background: rgba(145, 215, 227, 0.08);
  font: inherit;
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  transition: border-color 120ms ease, background 120ms ease, transform 120ms ease;
}

.metric-help-button:hover {
  border-color: rgba(145, 215, 227, 0.88);
  background: rgba(145, 215, 227, 0.16);
}

.metric-help-button:focus {
  outline: 2px solid rgba(145, 215, 227, 0.24);
  outline-offset: 2px;
}

.metric-help-button:active {
  transform: translateY(1px);
}

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

.total-card {
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-strong);
}

.total-card h3 {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 0.78rem;
}

.total-card p {
  margin: 0;
  font-size: 1.02rem;
  font-weight: 700;
}

.formula-dialog {
  width: min(1100px, calc(100% - 24px));
  max-height: calc(100dvh - 32px);
  border: 1px solid rgba(125, 196, 228, 0.22);
  border-radius: 22px;
  padding: 0;
  color: var(--text);
  background: linear-gradient(180deg, rgba(43, 48, 69, 0.98), rgba(24, 25, 38, 0.98));
  box-shadow: 0 28px 90px rgba(0, 0, 0, 0.45);
}

.formula-dialog::backdrop {
  background: rgba(8, 10, 18, 0.72);
  backdrop-filter: blur(4px);
}

.formula-dialog-body {
  padding: 18px;
  overflow: auto;
}

.formula-dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 16px;
}

.formula-dialog-kicker {
  margin: 0 0 4px;
  color: var(--ctp-sky);
  font-size: 0.75rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.formula-dialog-header h3 {
  margin: 0;
  font-size: 1.2rem;
}

.formula-dialog-overview {
  margin: 6px 0 0;
  max-width: 72ch;
  color: var(--muted);
  line-height: 1.5;
}

.formula-dialog-section + .formula-dialog-section {
  margin-top: 18px;
}

.formula-dialog-section h4 {
  margin: 0 0 10px;
  font-size: 0.92rem;
}

.shared-symbol-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px;
}

.shared-symbol-card,
.formula-side {
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px;
  background: rgba(36, 39, 58, 0.86);
}

.shared-symbol-heading,
.formula-step-header,
.formula-side-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.shared-symbol-heading strong,
.formula-step-header strong {
  font-size: 0.84rem;
}

.formula-unit {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 22px;
  padding: 0 8px;
  border-radius: 999px;
  color: var(--muted);
  background: rgba(148, 163, 184, 0.12);
  font-size: 0.72rem;
  font-weight: 700;
}

.formula-expression {
  margin-top: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 2px;
}

.formula-expression-display {
  font-size: 0.95rem;
}

.formula-expression-fallback {
  font-family: Consolas, "SFMono-Regular", monospace;
  font-size: 0.8rem;
  white-space: pre-wrap;
  color: var(--ctp-rosewater);
}

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

.formula-side-header {
  align-items: flex-end;
  margin-bottom: 10px;
}

.formula-side-header h4 {
  margin: 0;
  font-size: 0.98rem;
}

.formula-side-value {
  margin: 0;
  color: var(--ctp-green);
  font-size: 1rem;
  font-weight: 800;
}

.formula-steps {
  display: grid;
  gap: 10px;
  margin: 0;
  padding-left: 20px;
}

.formula-step {
  padding: 10px;
  border: 1px solid rgba(91, 96, 120, 0.72);
  border-radius: 14px;
  background: rgba(24, 25, 38, 0.72);
}

@media (max-width: 960px) {
  .layout {
    grid-template-columns: 1fr;
  }

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

  .formula-sides-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .page {
    width: min(100% - 20px, 100%);
    padding-top: 12px;
  }

  .panel {
    padding: 12px;
    border-radius: 16px;
  }

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

  .saved-inputs-grid {
    grid-template-columns: 1fr;
  }

  .saved-input-actions {
    grid-template-columns: 1fr 1fr;
  }

  .results-table th,
  .results-table td {
    padding: 6px 6px;
  }

  .formula-dialog {
    width: min(100% - 12px, 100%);
    max-height: calc(100dvh - 12px);
    border-radius: 18px;
  }

  .formula-dialog-body {
    padding: 14px;
  }

  .formula-dialog-header {
    flex-direction: column;
  }

  .shared-symbol-grid {
    grid-template-columns: 1fr;
  }
}
