:root {
  color-scheme: dark;
  --bg: #11131a;
  --card: #20232c;
  --field: #10131b;
  --text: #f4f6fb;
  --muted: #9aa1b2;
  --line: #424856;
  --line-soft: #333947;
  --accent: #98a7ff;
  --accent-soft: rgba(152, 167, 255, .12);
  --accent-border: rgba(152, 167, 255, .34);
  --best: #77e0a3;
  --best-soft: rgba(119, 224, 163, .12);
  --best-border: rgba(119, 224, 163, .34);
  --warning: #e7b55f;
  --toolbar: rgba(22, 25, 34, .97);
  --radius-card: 16px;
  --radius-field: 10px;
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html[data-theme="light"] {
  color-scheme: light;
  --bg: #f3f5fa;
  --card: #ffffff;
  --field: #ffffff;
  --text: #161923;
  --muted: #697286;
  --line: #cfd5e2;
  --line-soft: #e1e5ee;
  --accent: #405be8;
  --accent-soft: rgba(64, 91, 232, .10);
  --accent-border: rgba(64, 91, 232, .24);
  --best: #087443;
  --best-soft: rgba(8, 116, 67, .10);
  --best-border: rgba(8, 116, 67, .28);
  --warning: #9f6a00;
  --toolbar: rgba(255, 255, 255, .97);
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
}
body.has-fast-toolbar { padding-bottom: 76px; }
button, input, select { font: inherit; }
button, select { cursor: pointer; }
button:disabled { cursor: not-allowed; }

.app {
  width: min(860px, 100%);
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 8px 24px;
}

.calc-card, .bottom-panel {
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-card);
  background: var(--card);
}
.calc-card { padding: 16px 18px 12px; }
.bottom-panel { margin-top: 10px; overflow: hidden; }

.top-line {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}
.title-wrap { min-width: 0; display: grid; gap: 4px; }
h1 {
  margin: 0;
  font-size: clamp(24px, 5vw, 30px);
  line-height: 1.05;
  letter-spacing: -0.035em;
}
.mode-line { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.mode-inline { display: inline-flex; min-width: 0; }
.mode-inline select {
  width: auto;
  max-width: 220px;
  min-height: 30px;
  padding: 0 24px 0 0;
  border: 0;
  background: transparent;
  color: var(--accent);
  font-weight: 750;
  outline: none;
  -webkit-appearance: auto;
  appearance: auto;
}
.deal-master-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 30px;
  padding: 0 8px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: transparent;
  color: var(--accent);
  font-size: 14px;
  font-weight: 650;
  opacity: .9;
}
.deal-master-toggle.is-active {
  border-color: var(--accent-border);
  background: var(--accent-soft);
  opacity: 1;
}

.input-hint { margin: 6px 0 8px; color: var(--muted); font-size: 13px; line-height: 1.2; }
.input-hint[hidden] { display: none !important; }
.input-hint.is-active { color: var(--accent); }
.items { display: grid; }

.item-row {
  position: relative;
  display: grid;
  grid-template-columns: minmax(140px, 1fr) 34px minmax(124px, 160px) minmax(124px, 160px) minmax(132px, .85fr);
  gap: 8px;
  align-items: center;
  padding: 11px 0;
  border-top: 1px solid var(--line-soft);
}
.item-row:first-child { border-top: 0; padding-top: 0; }
.item-row.is-active:not(.is-best), .item-row.is-best {
  margin-left: -10px;
  margin-right: -10px;
  padding-left: 10px;
  padding-right: 10px;
  border-radius: 12px;
}
.item-row.is-active:not(.is-best) { background: var(--accent-soft); outline: 1px solid var(--accent-border); }
.item-row.is-best { background: var(--best-soft); outline: 1px solid var(--best-border); }
.name-cell { min-width: 0; }
.name-input {
  width: 100%;
  min-height: 34px;
  padding: 0 0 3px;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  outline: none;
  font-size: 18px;
}
.name-input:focus { color: var(--text); border-bottom-color: var(--accent); }

.input-box {
  min-width: 0;
  min-height: 48px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 4px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: var(--radius-field);
  background: var(--field);
}
.input-box:focus-within, .input-box.is-active-field {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.input-box input {
  min-width: 0;
  width: 100%;
  height: 46px;
  border: 0;
  background: transparent;
  color: var(--text);
  outline: none;
  font-size: 18px;
}
.suffix, .unit-select { color: var(--text); font-size: 17px; white-space: nowrap; }
.unit-select { max-width: 56px; height: 34px; padding: 0 1px; border: 0; background: transparent; outline: none; }

.result { min-width: 0; text-align: right; line-height: 1.05; }
.result-main { font-size: 18px; font-weight: 780; letter-spacing: -0.02em; white-space: nowrap; }
.result-sub { margin-top: 3px; color: var(--muted); font-size: 13px; font-weight: 650; white-space: nowrap; }
.item-row:not(.is-best) .result-main { color: color-mix(in srgb, var(--text) 88%, var(--muted)); }
.item-row.is-best .result-main, .item-row.is-best .result-sub { color: var(--best); }
.item-row.is-long-result .result-main { font-size: .92em; letter-spacing: -0.035em; }
.item-row.is-suspicious .result::after {
  content: "проверьте количество";
  display: block;
  margin-top: 5px;
  color: var(--warning);
  font-size: 12px;
  font-weight: 650;
}
@supports not (color: color-mix(in srgb, white, black)) {
  .item-row:not(.is-best) .result-main { color: var(--text); }
}

.deal-cell { grid-column: 1 / -1; display: grid; gap: 8px; margin-top: -2px; }
.deal-cell[hidden] { display: none !important; }
.deal-toggle {
  width: max-content;
  min-height: 32px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--accent);
  font-weight: 650;
  text-align: left;
}
.deal-controls {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(110px, .45fr) 32px;
  gap: 8px;
  align-items: end;
}
.deal-controls[hidden] { display: none; }
.deal-select-wrap, .deal-percent-wrap { display: grid; gap: 4px; color: var(--muted); font-size: 12px; }
.deal-select, .deal-percent-input {
  min-height: 36px;
  border: 1px solid var(--line-soft);
  border-radius: 10px;
  background: rgba(16, 19, 27, .62);
  color: var(--text);
}
.deal-select { width: 100%; padding: 0 10px; outline: none; }
.deal-percent-input { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 4px; padding: 0 10px; }
.deal-percent-input input { width: 100%; height: 34px; border: 0; background: transparent; color: var(--text); outline: none; }
.deal-clear {
  width: 32px;
  height: 36px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line-soft);
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
}
.deal-select:focus, .deal-percent-input:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.summary-line {
  margin-top: 10px;
  padding: 10px 12px;
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  background: rgba(152, 167, 255, .06);
  color: var(--muted);
  font-size: 15px;
  line-height: 1.35;
}
.summary-line.is-ready { color: var(--text); }
.summary-line.is-equal { color: var(--accent); }

.primary-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto 42px;
  align-items: center;
  gap: 10px;
  padding-top: 10px;
}
.secondary-actions { display: grid; grid-template-columns: 1fr; gap: 8px; margin-top: 8px; }
.text-button {
  min-height: 38px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--accent);
  font-weight: 650;
}
.text-button:disabled, .share-icon-button:disabled { opacity: .42; cursor: not-allowed; }
.text-button:disabled:active, .share-icon-button:disabled:active { transform: none; }
.copy-action { display: inline-flex; align-items: center; gap: 8px; justify-content: flex-end; text-align: right; white-space: nowrap; }
.copy-icon { font-size: 16px; line-height: 1; }
.share-icon-button {
  width: 42px;
  height: 42px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--accent);
  font: inherit;
  line-height: 1;
}
.share-icon {
  width: 22px;
  height: 22px;
  display: block;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.share-icon-button:active { transform: scale(.96); }
.reset-wide-button {
  width: 100%;
  min-height: 46px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-weight: 650;
  text-align: center;
}
.reset-wide-button:disabled { opacity: .42; cursor: not-allowed; }
.reset-wide-button:disabled:active { transform: none; }
.action-hint {
  display: inline-flex;
  align-items: center;
  width: max-content;
  margin-top: 8px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 13px;
  line-height: 1.2;
}
.action-hint[hidden] { display: none !important; }

.icon-button {
  width: 32px;
  height: 32px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line-soft);
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  transition: transform .15s ease, color .15s ease, border-color .15s ease;
}
.icon-button:active, .text-button:active, .panel-button:active, .fast-toolbar__button:active { transform: scale(.98); }
.icon-button:disabled { display: none; }
.remove-item { font-size: 23px; line-height: 1; }
.theme-toggle { flex: 0 0 auto; }

.panel-button {
  width: 100%;
  min-height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 18px;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 18px;
  text-align: left;
}
.panel-hint { color: var(--muted); font-size: 13px; }
.about-content { padding: 0 18px 16px; color: var(--muted); line-height: 1.4; font-size: 14px; }
.about-content p { margin: 0 0 10px; }
.about-content .status-line { min-height: 0; padding: 8px 0 0; border-top: 1px solid var(--line-soft); font-size: 14px; }
.status-line { min-height: 52px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 0 18px; color: var(--text); font-size: 16px; }

.confirm-panel[hidden], .copy-dialog[hidden], .fast-toolbar[hidden] { display: none !important; }
.confirm-panel {
  position: fixed;
  left: 10px;
  right: 10px;
  bottom: calc(10px + env(safe-area-inset-bottom));
  z-index: 2100;
}
.confirm-panel__box {
  width: min(560px, 100%);
  margin: 0 auto;
  display: grid;
  gap: 14px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--toolbar);
  color: var(--text);
  box-shadow: 0 18px 54px rgba(0, 0, 0, .42);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}
.confirm-panel__title { font-size: 16px; font-weight: 750; }
.confirm-panel__text { margin-top: 3px; color: var(--muted); font-size: 13px; line-height: 1.3; }
.confirm-panel__actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.confirm-panel__actions .text-button { min-height: 48px; border-radius: 14px; padding: 0 12px; text-align: center; }
#cancelReset { border: 1px solid var(--line-soft); background: transparent; color: var(--muted); }
#confirmResetButton { border: 1px solid var(--accent-border); background: var(--accent); color: #fff; font-weight: 800; }

.copy-dialog {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: grid;
  place-items: end center;
  padding: 16px;
  background: rgba(0, 0, 0, .42);
}
.copy-dialog__box {
  width: min(560px, 100%);
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--card);
  color: var(--text);
  box-shadow: 0 18px 54px rgba(0, 0, 0, .42);
}
.copy-dialog__box h2 { margin: 0 0 8px; font-size: 22px; line-height: 1.15; }
.copy-dialog__box p { margin: 0 0 12px; color: var(--muted); line-height: 1.35; }
.copy-dialog textarea {
  width: 100%;
  min-height: 150px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--field);
  color: var(--text);
  font: inherit;
  line-height: 1.4;
  resize: vertical;
  outline: none;
}
.copy-dialog textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.copy-dialog__actions { display: flex; justify-content: space-between; gap: 12px; margin-top: 10px; }

.fast-toolbar {
  position: fixed;
  left: 10px;
  right: 10px;
  top: var(--toolbar-top, auto);
  bottom: auto;
  z-index: 1000;
  min-height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 8px 8px 14px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--toolbar);
  color: var(--text);
  box-shadow: 0 12px 32px rgba(0, 0, 0, .34);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  transform: translateZ(0);
}
.fast-toolbar__status { flex: 1 1 auto; min-width: 0; color: var(--muted); font-size: 14px; line-height: 1.2; }
.fast-toolbar__actions { flex: 0 0 auto; display: inline-flex; align-items: center; gap: 8px; }
.fast-toolbar__button, .fast-toolbar__back {
  min-height: 40px;
  padding: 0 14px;
  border-radius: 999px;
  font: inherit;
  font-weight: 700;
}
.fast-toolbar__button { min-width: 96px; border: 0; background: var(--accent); color: #fff; }
.fast-toolbar__back { min-width: 78px; border: 1px solid var(--line); background: transparent; color: var(--muted); }
.fast-toolbar__back:disabled { opacity: .38; cursor: not-allowed; }
.fast-toolbar__back:active:not(:disabled) { transform: scale(.98); }

.sr-only {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

@media screen and (max-width: 620px) {
  .app { padding-left: 0; padding-right: 0; }
  .calc-card, .bottom-panel { border-left: 0; border-right: 0; border-radius: 14px; }
  .calc-card { padding: 14px 28px 12px; }
  .item-row { grid-template-columns: 1fr 1fr; gap: 8px 10px; padding: 11px 0; }
  .item-row.is-active:not(.is-best), .item-row.is-best { margin-left: -8px; margin-right: -8px; padding-left: 8px; padding-right: 8px; }
  .name-cell { grid-column: 1 / -1; padding-right: 0; }
  .name-input { min-height: 27px; font-size: 18px; }
  .remove-item { position: absolute; top: 8px; right: 0; }
  .input-box { min-height: 54px; padding: 0 12px; }
  .input-box input { height: 52px; font-size: 22px; }
  .suffix, .unit-select { font-size: 20px; }
  .unit-select { max-width: 62px; }
  .result { grid-column: 1 / -1; min-height: 27px; text-align: right; }
  .result-main { display: inline; font-size: 21px; }
  .result-sub { display: inline; margin-top: 0; margin-left: 7px; font-size: 15px; }
  .item-row.is-long-result .result-main { font-size: 19px; }
  .item-row.is-suspicious .result::after { font-size: 11px; }
  .deal-controls { grid-template-columns: minmax(0, 1fr) minmax(96px, .48fr) 32px; gap: 8px; }
  .deal-select, .deal-percent-input, .deal-clear { min-height: 34px; }
  .mode-line { gap: 8px; }
  .deal-master-toggle { padding: 0 8px; font-size: 14px; }
  .text-button { min-height: 36px; font-size: 16px; }
  .panel-button, .status-line { min-height: 48px; padding-left: 28px; padding-right: 28px; }
}

@media screen and (max-width: 390px) {
  .calc-card { padding-left: 18px; padding-right: 18px; }
  h1 { font-size: 23px; }
  .mode-inline select { font-size: 18px; }
  .primary-actions { grid-template-columns: minmax(0, 1fr) auto 40px; gap: 8px; }
  .share-icon-button { width: 40px; height: 40px; }
  .input-box { min-height: 50px; padding: 0 10px; }
  .input-box input { height: 48px; font-size: 21px; }
  .result-main { font-size: 20px; }
  .result-sub { font-size: 14px; }
  .panel-button, .status-line { padding-left: 18px; padding-right: 18px; }
  .fast-toolbar { left: 8px; right: 8px; align-items: stretch; }
  .fast-toolbar__status { font-size: 13px; }
  .fast-toolbar__back { min-width: 68px; padding: 0 10px; }
  .fast-toolbar__button { min-width: 86px; }
}

/* v1.1: desktop rows with remove button keep a stable grid */
@media screen and (min-width: 621px) {
  .remove-item {
    width: 32px;
    height: 32px;
    justify-self: center;
  }

  .remove-item:disabled {
    visibility: hidden;
    pointer-events: none;
  }

  .result {
    justify-self: end;
    max-width: 100%;
  }
}

/* v1.2: keep the hidden delete column on desktop so first input does not collapse */
@media screen and (min-width: 621px) {
  .remove-item:disabled {
    display: inline-grid;
    visibility: hidden;
    pointer-events: none;
  }
}
