:root {
  --bg: #EEF1F4;
  --surface: #FFFFFF;
  --well: #F1F4F7;
  --ink: #14202B;
  --muted: #5B6B7A;
  --line: #E1E6EB;
  --line-strong: #C9D2DA;
  --accent: #F5B700;
  --accent-ink: #1E1600;
  --required: #E0483D;
  --required-soft: #FDF1F0;
  --ring: rgba(245, 183, 0, 0.45);
  --shadow: 0 1px 2px rgba(20, 32, 43, 0.05), 0 12px 32px -16px rgba(20, 32, 43, 0.22);
  --radius: 10px;
  --ease: cubic-bezier(0.3, 0.7, 0.2, 1);
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Onest", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
}

.page {
  max-width: 720px;
  margin: 0 auto;
  padding: 40px 16px 56px;
}

.card {
  background: var(--surface);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 32px 32px 36px;
}

@media (max-width: 560px) {
  .page { padding: 12px 8px 32px; }
  .card { padding: 22px 14px 26px; border-radius: 16px; }
}

.title {
  margin: 0;
  text-align: center;
  font-size: 30px;
  line-height: 1.15;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.hint {
  margin: 8px auto 26px;
  max-width: 44ch;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

#calc { display: grid; gap: 16px; }

.field { margin: 0; padding: 0; border: 0; min-width: 0; position: relative; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.seg-label {
  padding: 0;
  margin-bottom: 7px;
  font-size: 15px;
  font-weight: 600;
}

.field-label {
  display: block;
  margin-bottom: 6px;
  font-size: 14px;
  font-weight: 500;
  color: var(--muted);
}

.field-note { margin: 6px 0 0; font-size: 13px; color: var(--muted); }

.field-error { margin: 5px 0 0; font-size: 13px; color: var(--required); }
.field-error:empty { display: none; }

/* Segmented control with a sliding thumb; --i is set from JS to the checked index. */
.seg {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--n), minmax(0, 1fr));
  background: var(--well);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 3px;
}

.seg input { position: absolute; opacity: 0; pointer-events: none; }

.seg label {
  position: relative;
  z-index: 1;
  padding: 10px 4px;
  text-align: center;
  font-size: 15px;
  font-weight: 500;
  color: var(--muted);
  border-radius: 9px;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color 0.2s, opacity 0.2s;
}

.seg label:hover { color: var(--ink); }
.seg input:checked + label { color: var(--accent-ink); font-weight: 600; }
.seg input:focus-visible + label { box-shadow: 0 0 0 3px var(--ring); }
.seg input:disabled + label { opacity: 0.35; cursor: not-allowed; }

.seg-thumb {
  position: absolute;
  top: 3px;
  bottom: 3px;
  left: 3px;
  width: calc((100% - 6px) / var(--n));
  transform: translateX(calc(var(--i) * 100%));
  background: var(--accent);
  border-radius: 9px;
  box-shadow: 0 1px 3px rgba(30, 22, 0, 0.2);
  transition: transform 0.28s var(--ease);
}

@media (max-width: 420px) {
  .seg label { font-size: 13.5px; }
}

.grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px 16px;
  margin-top: 4px;
}

@media (max-width: 560px) {
  .grid { grid-template-columns: minmax(0, 1fr); gap: 12px; }
}

.input-wrap { position: relative; }

.input {
  width: 100%;
  height: 46px;
  padding: 0 14px;
  font: inherit;
  font-size: 16px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s, background-color 0.15s;
}

.input::placeholder { color: var(--muted); opacity: 0.55; }
.input:hover { border-color: var(--muted); }
.input:focus { border-color: var(--ink); box-shadow: 0 0 0 3px var(--ring); }
.input:disabled { background: var(--well); border-color: var(--line); cursor: not-allowed; }
.input:disabled::placeholder { opacity: 1; font-size: 14px; }

.money .input { padding-left: 28px; }
.money::before {
  content: "$";
  position: absolute;
  left: 13px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  pointer-events: none;
}

.field.required .input { border-color: var(--required); }
.field.invalid .input { border-color: var(--required); background: var(--required-soft); }
.field.required .input:focus, .field.invalid .input:focus { box-shadow: 0 0 0 3px rgba(224, 72, 61, 0.2); }

.combo-wrap .input { padding-right: 40px; text-overflow: ellipsis; }
.combo-caret {
  position: absolute;
  right: 15px;
  top: 50%;
  width: 12px;
  height: 8px;
  margin-top: -4px;
  color: var(--muted);
  pointer-events: none;
  transition: transform 0.2s var(--ease);
}
.combo.open .combo-caret { transform: rotate(180deg); }

.combo-list {
  position: absolute;
  z-index: 20;
  left: 0;
  right: 0;
  top: 100%;
  max-height: 320px;
  overflow-y: auto;
  margin: 6px 0 0;
  padding: 6px;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overscroll-behavior: contain;
  animation: pop 0.16s var(--ease);
}

.combo-list li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 15px;
}
.combo-list li .port { color: var(--muted); font-size: 13px; white-space: nowrap; }
.combo-list li[aria-selected="true"] { font-weight: 600; }
.combo-list li.active { background: var(--well); }
.combo-list li.empty { color: var(--muted); cursor: default; justify-content: center; }

@keyframes pop {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: none; }
}

.action-cell { display: flex; align-items: flex-end; }

.action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  height: 46px;
  padding: 0 12px;
  font: inherit;
  font-size: 14.5px;
  font-weight: 600;
  color: var(--surface);
  background: var(--ink);
  border: 0;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background-color 0.15s, opacity 0.15s, transform 0.1s;
}
.action-btn svg { width: 16px; height: 16px; flex: none; }
.action-btn:hover:not(:disabled) { background: #22323F; }
.action-btn:active:not(:disabled) { transform: translateY(1px); }
.action-btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--ring); }
.action-btn:disabled { opacity: 0.3; cursor: not-allowed; }

.reset-btn { color: var(--ink); background: var(--surface); border: 1px solid var(--line-strong); }
.reset-btn:hover:not(:disabled) { background: var(--well); border-color: var(--muted); }

.result {
  position: relative;
  margin-top: 28px;
  padding: 24px 24px 20px;
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  isolation: isolate;
}

.result::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: var(--logo) center / 62% auto no-repeat;
  opacity: 0.13;
  pointer-events: none;
}

@media (max-width: 560px) {
  .result { padding: 18px 12px 14px; }
  .result::before { background-size: 82% auto; }
}

.progress {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  overflow: hidden;
  opacity: 0;
  transition: opacity 0.2s;
}
.progress::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 35%;
  background: var(--accent);
  border-radius: 2px;
  animation: slide 1s var(--ease) infinite;
}
.is-loading .progress { opacity: 1; }

@keyframes slide {
  from { left: -35%; }
  to { left: 100%; }
}

h2 { margin: 0 0 14px; font-size: 20px; font-weight: 600; letter-spacing: -0.01em; }

.total {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 64px;
  padding: 12px 18px;
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 12px;
}
.total-label { font-size: 18px; font-weight: 700; }
.total-amount { font-size: 32px; line-height: 1; font-weight: 700; letter-spacing: -0.02em; }
.total-bottom { margin-top: 12px; }

.total[data-state="empty"] { background: var(--well); }
.total[data-state="empty"] .total-label::before,
.total[data-state="empty"] .total-amount::before {
  content: "";
  display: inline-block;
  width: 64px;
  height: 14px;
  border-radius: 6px;
  background: var(--line);
  vertical-align: middle;
}
.total[data-state="empty"] .total-amount::before { width: 120px; height: 26px; }

.total-amount, .lines .amount { transition: opacity 0.15s; }
.is-loading .total[data-state="ready"] .total-amount,
.is-loading .lines .amount { opacity: 0.4; }

@media (max-width: 420px) {
  .total-amount { font-size: 27px; }
}

.result-message { margin: 14px 0 0; font-size: 15px; color: var(--muted); }
.result-message:empty { display: none; }
.result-message.error { color: var(--required); }

.lines { list-style: none; margin: 10px 0 0; padding: 0; }

.lines .row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 7px 4px;
  font-size: 17px;
}
.lines .row .label { min-width: 0; }
.lines .row .leader {
  flex: 1;
  min-width: 16px;
  border-bottom: 1.5px dotted var(--line-strong);
  transform: translateY(-4px);
}
.lines .row .amount { white-space: nowrap; font-weight: 500; }

.lines .row.subtotal {
  margin: 2px 0 8px;
  padding-top: 9px;
  border-top: 1px solid var(--line-strong);
  font-weight: 700;
}
.lines .row.subtotal .leader { visibility: hidden; }
.lines .row.subtotal .amount { font-weight: 700; }

.lines .row.entering { animation: row-in 0.32s var(--ease); }

@keyframes row-in {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: none; }
}

@media (max-width: 560px) {
  .lines .row { font-size: 16px; padding: 6px 2px; }
}

.skeleton .row { justify-content: space-between; padding: 10px 4px; }
.sk {
  display: block;
  height: 14px;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--well) 0%, var(--line) 50%, var(--well) 100%);
  background-size: 200% 100%;
  animation: shimmer 1.3s linear infinite;
}
.sk-amount { width: 64px; }

@keyframes shimmer {
  from { background-position: 100% 0; }
  to { background-position: -100% 0; }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  padding: 11px 18px;
  background: var(--ink);
  color: var(--surface);
  border-radius: 10px;
  font-size: 15px;
  box-shadow: var(--shadow);
  animation: toast-in 0.22s var(--ease);
}

@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
