/* Settings */
:root {
  color-scheme: light;
  --font-sans: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;

  --color-page: #f6f7f9;
  --color-surface: #ffffff;
  --color-surface-muted: #f8fafc;
  --color-surface-soft: #eef3f8;
  --color-surface-soft-hover: #dfe8f2;
  --color-table-head: #edf1f5;
  --color-border: #d9dee6;
  --color-border-soft: #e2e7ee;
  --color-border-muted: #e7ebf0;
  --color-text: #243241;
  --color-heading: #2d3a45;
  --color-muted: #5d6b7a;
  --color-muted-strong: #526172;
  --color-muted-soft: #9aa7b4;
  --color-accent: #0b63ce;
  --color-accent-hover: #084f9f;
  --color-success-bg: #d9f4e4;
  --color-success-text: #146c43;
  --color-danger-bg: #fde2e1;
  --color-danger-text: #9f2a25;
  --color-error-text: #8a2a22;

  --space-2xs: 2px;
  --space-xs: 3px;
  --space-sm: 6px;
  --space-md: 8px;
  --space-table-y: 9px;
  --space-lg: 10px;
  --space-xl: 12px;
  --space-2xl: 14px;
  --space-gap-wide: 16px;
  --space-3xl: 18px;
  --space-4xl: 24px;
  --space-5xl: 28px;

  --radius-sm: 6px;
  --radius-pill: 999px;

  --font-size-xs: 11px;
  --font-size-sm: 12px;
  --font-size-md: 13px;
  --font-size-lg: 14px;
  --font-size-xl: 15px;
  --font-size-2xl: 16px;
  --font-size-3xl: 17px;
  --font-size-title: 24px;
  --font-size-title-mobile: 20px;

  --content-list: 1280px;
  --content-protocol: 1040px;
  --control-height: 34px;
  --control-height-sm: 30px;
  --status-min-width: 82px;
  --bin-col-width: 120px;
  --reason-cell-min-width: 240px;
  --mobile-label-col-width: 92px;
  --mobile-action-min-width: 120px;
  --mobile-score-col-min: 108px;
  --mobile-price-col-min: 90px;
  --mobile-grid-gap: 9px;
  --primary-cell-padding-y: 7px;
  --details-arrow-y: 5px;
  --details-arrow-x: 7px;
  --badge-height: 24px;
  --badge-height-mobile: 22px;
  --section-summary-height-mobile: 32px;
  --error-max-width: 300px;

  font-family: var(--font-sans);
  background: var(--color-page);
  color: var(--color-text);
}

/* Base */
* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

body.dialog-open {
  position: fixed;
  right: 0;
  left: 0;
  width: 100%;
  overflow: hidden;
}

/* Layout */
.list-page {
  padding: var(--space-4xl);
}

.page-is-loading {
  cursor: progress;
}

.auth-page {
  min-height: 100vh;
  padding: var(--space-4xl);
  display: flex;
  align-items: center;
  justify-content: center;
}

.protocol-page {
  padding-top: calc(var(--space-3xl) + var(--tg-content-safe-area-inset-top, 0px));
  padding-right: calc(var(--space-3xl) + var(--tg-content-safe-area-inset-right, 0px));
  padding-bottom: calc(var(--space-3xl) + var(--tg-content-safe-area-inset-bottom, 0px));
  padding-left: calc(var(--space-3xl) + var(--tg-content-safe-area-inset-left, 0px));
}

main {
  margin: 0 auto;
}

.list-page main {
  max-width: var(--content-list);
}

.protocol-page main {
  max-width: var(--content-protocol);
}

.auth-page main {
  width: min(100%, 520px);
}

header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-gap-wide);
  margin-bottom: var(--space-3xl);
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-xl);
  margin-bottom: var(--space-2xl);
}

.actions {
  display: flex;
  gap: var(--space-md);
  flex-wrap: wrap;
  justify-content: flex-end;
}

.page-shell {
  position: relative;
}

.page-loading {
  position: fixed;
  top: var(--tg-content-safe-area-inset-top, 0px);
  right: 0;
  left: 0;
  z-index: 1000;
  height: 3px;
  overflow: hidden;
  background: transparent;
  opacity: 0;
  transition: opacity 120ms ease;
  pointer-events: none;
}

.page-is-loading .page-loading,
.htmx-request.page-loading,
.htmx-request .page-loading {
  opacity: 1;
}

.page-loading-bar {
  width: 28%;
  height: 100%;
  border-radius: 0 var(--radius-pill) var(--radius-pill) 0;
  background: linear-gradient(90deg, #0b63ce 0%, #4ca0ff 100%);
  transform: translateX(-120%);
}

.page-is-loading .page-loading-bar,
.htmx-request .page-loading-bar {
  animation: list-loading-slide 900ms ease-in-out infinite;
}

#page-root-content,
#protocol-list-content {
  transition: opacity 120ms ease;
}

.page-is-loading #page-root-content,
.page-is-loading #protocol-list-content {
  opacity: 0.72;
}

/* Typography */
h1 {
  margin: 0;
  font-size: var(--font-size-title);
  line-height: 1.2;
  letter-spacing: 0;
}

.protocol-page h1 {
  margin-bottom: var(--space-xs);
}

h2 {
  margin: 0 0 var(--space-lg);
  font-size: var(--font-size-3xl);
  letter-spacing: 0;
}

h3 {
  margin: var(--space-2xl) 0 var(--space-md);
  font-size: var(--font-size-xl);
  letter-spacing: 0;
}

.meta,
.muted {
  color: var(--color-muted);
}

.meta {
  font-size: var(--font-size-lg);
}

.muted {
  font-size: var(--font-size-md);
}

a {
  color: var(--color-accent);
  font-weight: 700;
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

@keyframes list-loading-slide {
  0% {
    transform: translateX(-120%);
  }
  100% {
    transform: translateX(320%);
  }
}

/* Components */
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--control-height);
  padding: 0 var(--space-xl);
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--color-accent);
  color: var(--color-surface);
  cursor: pointer;
  font-family: inherit;
  font-size: var(--font-size-lg);
  font-weight: 700;
  text-decoration: none;
}

.list-page .button {
  min-height: var(--control-height-sm);
  padding: 0 var(--space-lg);
  font-size: var(--font-size-md);
}

.button:hover {
  background: var(--color-accent-hover);
  text-decoration: none;
}

.button.secondary {
  background: var(--color-surface-soft);
  color: var(--color-accent);
}

.button.secondary:hover {
  background: var(--color-surface-soft-hover);
}

.auth-card {
  padding: clamp(24px, 5vw, 36px);
  border: 1px solid var(--color-border);
  border-radius: 18px;
  background: linear-gradient(180deg, #fff 0%, #f5f9ff 100%);
  box-shadow: 0 20px 50px rgb(36 50 65 / 10%);
}

.auth-eyebrow {
  margin-bottom: var(--space-lg);
  color: var(--color-accent);
  font-size: var(--font-size-sm);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.auth-copy {
  margin: var(--space-lg) 0 var(--space-3xl);
  color: var(--color-muted);
  font-size: var(--font-size-xl);
  line-height: 1.5;
}

.auth-meta {
  margin-top: var(--space-xl);
  color: var(--color-muted-strong);
  font-size: var(--font-size-lg);
}

.auth-status {
  padding: var(--space-xl) var(--space-2xl);
  border: 1px solid var(--color-border);
  border-radius: 14px;
  background: var(--color-surface);
  color: var(--color-heading);
  font-size: var(--font-size-lg);
  font-weight: 700;
}

.auth-status.error {
  border-color: rgb(159 42 37 / 18%);
  background: var(--color-danger-bg);
  color: var(--color-danger-text);
}

.search-bar {
  display: grid;
  grid-template-columns: auto minmax(220px, 360px) auto auto;
  align-items: center;
  gap: var(--space-md);
  margin-bottom: var(--space-xl);
  padding: var(--space-xl);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
}

.search-bar label {
  color: var(--color-heading);
  font-size: var(--font-size-lg);
  font-weight: 700;
}

.search-bar input {
  min-width: 0;
  height: var(--control-height);
  padding: 0 var(--space-xl);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  color: var(--color-text);
  font: inherit;
  font-size: var(--font-size-lg);
}

.search-bar input:focus {
  border-color: var(--color-accent);
  outline: 2px solid rgb(11 99 206 / 18%);
  outline-offset: 1px;
}

.search-summary {
  margin: 0 0 var(--space-lg);
  color: var(--color-muted-strong);
  font-size: var(--font-size-lg);
  font-weight: 700;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-lg);
  margin-top: var(--space-xl);
  padding: var(--space-xl);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
}

.pagination-top {
  margin: 0 0 var(--space-xl);
}

.pagination-summary {
  color: var(--color-muted-strong);
  font-size: var(--font-size-lg);
  font-weight: 700;
}

.pagination-actions {
  display: flex;
  gap: var(--space-md);
  flex-wrap: wrap;
}

.button.disabled {
  background: var(--color-surface-soft);
  color: var(--color-muted-soft);
  cursor: default;
  pointer-events: none;
}

table {
  width: 100%;
  border-collapse: collapse;
  border: 1px solid var(--color-border-soft);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.list-page table {
  background: var(--color-surface);
  border-color: var(--color-border);
}

th,
td {
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid var(--color-border-muted);
  text-align: left;
  vertical-align: top;
  font-size: var(--font-size-md);
}

.list-page th,
.list-page td {
  padding: var(--space-lg) var(--space-xl);
  font-size: var(--font-size-lg);
  white-space: nowrap;
}

th {
  background: var(--color-table-head);
  color: var(--color-heading);
  font-weight: 700;
}

.list-page th {
  font-weight: 600;
}

tr:last-child td {
  border-bottom: 0;
}

.status {
  display: inline-block;
  min-width: var(--status-min-width);
  padding: var(--space-xs) var(--space-md);
  border-radius: var(--radius-pill);
  text-align: center;
  font-size: var(--font-size-sm);
  font-weight: 700;
  text-transform: uppercase;
}

.succeeded,
.winner {
  background: var(--color-success-bg);
  color: var(--color-success-text);
}

.failed {
  background: var(--color-danger-bg);
  color: var(--color-danger-text);
}

.error {
  max-width: var(--error-max-width);
  white-space: normal;
  color: var(--color-error-text);
}

.empty {
  color: var(--color-muted);
  font-size: var(--font-size-lg);
}

.list-page .empty {
  padding: var(--space-5xl);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
}

.panel {
  margin-bottom: var(--space-xl);
  padding: var(--space-2xl);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
}

.grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--space-lg);
}

.field {
  min-width: 0;
}

.headline-field {
  margin-bottom: var(--space-xl);
  padding-bottom: var(--space-xl);
  border-bottom: 1px solid var(--color-border-muted);
}

.label {
  margin-bottom: var(--space-2xs);
  color: var(--color-muted);
  font-size: var(--font-size-xs);
  font-weight: 800;
  text-transform: uppercase;
}

.value {
  overflow-wrap: anywhere;
  font-size: var(--font-size-lg);
  line-height: 1.35;
}

/* Protocol page */
.lot-title {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-xl);
  margin-bottom: var(--space-lg);
}

.lot-title strong {
  display: block;
  font-size: var(--font-size-2xl);
  line-height: 1.3;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: var(--badge-height);
  padding: 0 var(--space-md);
  border-radius: var(--radius-pill);
  background: var(--color-surface-soft);
  color: var(--color-heading);
  font-size: var(--font-size-sm);
  font-weight: 800;
  white-space: nowrap;
}

.protocol-page .bin-col {
  width: var(--bin-col-width);
  white-space: nowrap;
}

.protocol-page td.bin-col {
  color: var(--color-muted);
}

.protocol-page .unit-price-cell,
.protocol-page .article13-cell,
.protocol-page .amount-cell {
  white-space: nowrap;
}

.protocol-page .offer-values {
  display: none;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
}

.section-summary {
  margin: var(--space-2xl) 0 var(--space-md);
  color: var(--color-muted-strong);
  font-size: var(--font-size-xl);
  font-weight: 650;
  letter-spacing: 0;
  cursor: pointer;
}

.lot-details + .lot-details .section-summary {
  margin-top: var(--space-md);
}

.compact-list {
  margin: var(--space-sm) 0 0;
  padding-left: var(--space-3xl);
}

.compact-list li + li {
  margin-top: 5px;
}

.reason-cell {
  min-width: var(--reason-cell-min-width);
}

.reason-summary {
  display: block;
}

.reason-details-button {
  min-height: var(--control-height-sm);
  margin-top: var(--space-sm);
  padding: 0 var(--space-lg);
  font-size: var(--font-size-md);
}

.reason-dialog {
  margin: auto;
  width: min(640px, calc(100vw - var(--space-4xl)));
  max-height: min(680px, calc(100vh - var(--space-4xl)));
  padding: 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  color: var(--color-text);
  overscroll-behavior: contain;
  overflow: auto;
}

.reason-dialog::backdrop {
  background: rgb(36 50 65 / 45%);
}

.dialog-header {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: var(--space-xl);
  padding: var(--space-xl);
  border-bottom: 1px solid var(--color-border-muted);
  background: var(--color-surface-muted);
}

.dialog-close {
  min-height: var(--control-height-sm);
  padding: 0 var(--space-lg);
  font-size: var(--font-size-md);
  white-space: nowrap;
}

.dialog-body {
  padding: var(--space-xl);
}

.reason-dialog-summary {
  margin: 0 0 var(--space-md);
  font-weight: 700;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: var(--space-xl);
  padding-top: var(--space-xl);
  border-top: 1px solid var(--color-border-muted);
}

@media (max-width: 760px) and (orientation: portrait) {
  .reason-dialog {
    inset: 0;
    width: 100vw;
    max-width: 100vw;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    margin: 0;
    border: 0;
    border-radius: 0;
  }

  .reason-dialog .dialog-header {
    position: sticky;
    top: 0;
    z-index: 1;
  }

  .reason-dialog .dialog-footer {
    display: none;
  }
}

/* Mobile protocol layout */
@media (max-width: 760px), (orientation: landscape) and (max-height: 520px) {
  .protocol-page {
    padding-top: var(--tg-content-safe-area-inset-top, 0px);
    padding-right: var(--tg-content-safe-area-inset-right, 0px);
    padding-bottom: var(--tg-content-safe-area-inset-bottom, 0px);
    padding-left: var(--tg-content-safe-area-inset-left, 0px);
  }

  .protocol-page main {
    max-width: none;
  }

  .top {
    display: block;
    padding: var(--space-xl);
  }

  .actions {
    justify-content: flex-start;
    margin-top: var(--space-lg);
  }

  .protocol-page .button {
    flex: 1 1 var(--mobile-action-min-width);
  }

  h1 {
    font-size: var(--font-size-title-mobile);
  }

  .panel {
    margin-bottom: var(--space-md);
    padding: var(--space-xl);
    border-left: 0;
    border-right: 0;
    border-radius: 0;
  }

  .grid {
    grid-template-columns: 1fr 1fr;
    gap: var(--mobile-grid-gap);
  }

  .lot-title {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: start;
    gap: var(--space-md);
    margin-bottom: var(--space-md);
  }

  .lot-title strong {
    font-size: var(--font-size-xl);
  }

  .lot-title .muted {
    margin-bottom: var(--space-xs);
  }

  .badge {
    margin-top: 0;
    min-height: var(--badge-height-mobile);
    font-size: var(--font-size-xs);
  }

  .section-summary {
    display: flex;
    align-items: center;
    gap: var(--space-md);
    min-height: var(--section-summary-height-mobile);
    margin: var(--space-xl) 0 0;
    padding: 0 var(--space-2xs);
    font-size: var(--font-size-lg);
  }

  .lot-details + .lot-details .section-summary {
    margin-top: var(--space-sm);
  }

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

  .section-summary::before {
    width: 0;
    height: 0;
    border-top: var(--details-arrow-y) solid transparent;
    border-bottom: var(--details-arrow-y) solid transparent;
    border-left: var(--details-arrow-x) solid var(--color-muted-soft);
    content: "";
    transition: transform 0.15s ease;
  }

  .lot-details[open] .section-summary::before {
    transform: rotate(90deg);
  }

  .lot-details[open] .section-summary {
    margin-bottom: var(--space-md);
  }

  .protocol-page table,
  .protocol-page thead,
  .protocol-page tbody,
  .protocol-page tr,
  .protocol-page th,
  .protocol-page td {
    display: block;
  }

  .protocol-page table {
    border: 0;
  }

  .protocol-page thead {
    display: none;
  }

  .protocol-page tr {
    margin-bottom: var(--space-md);
    border: 1px solid var(--color-border-soft);
    border-radius: var(--radius-sm);
    overflow: hidden;
  }

  .protocol-page td {
    display: grid;
    grid-template-columns: var(--mobile-label-col-width) minmax(0, 1fr);
    gap: var(--space-md);
    padding: var(--space-table-y) var(--space-xl);
  }

  .protocol-page td::before {
    content: attr(data-label);
    color: var(--color-muted);
    font-size: var(--font-size-xs);
    font-weight: 800;
    text-transform: uppercase;
  }

  .protocol-page td.primary-cell {
    display: block;
    padding: var(--primary-cell-padding-y) var(--space-xl);
    background: var(--color-surface-muted);
    font-weight: 700;
  }

  .protocol-page td.primary-cell::before,
  .protocol-page td.reason-cell::before,
  .protocol-page td.offer-values::before {
    content: none;
  }

  .protocol-page td.reason-cell {
    display: block;
  }

  .protocol-page td.reason-cell .compact-list {
    list-style: none;
    padding-left: 0;
  }

  .protocol-page tr.price-calculation-row,
  .protocol-page tr.pitanie-score-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(var(--mobile-score-col-min), 1fr));
  }

  .protocol-page tr.price-calculation-row .primary-cell,
  .protocol-page tr.pitanie-score-row .primary-cell {
    grid-column: 1 / -1;
  }

  .protocol-page td.price-calculation-cell,
  .protocol-page td.pitanie-score-cell {
    display: block;
  }

  .protocol-page td.price-calculation-cell::before,
  .protocol-page td.pitanie-score-cell::before {
    display: block;
    margin-bottom: var(--space-2xs);
  }

  .protocol-page .bin-col,
  .protocol-page .unit-price-cell,
  .protocol-page .article13-cell,
  .protocol-page .amount-cell {
    display: none;
  }

  .protocol-page td.offer-values {
    display: block;
  }

  .price-line {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(var(--mobile-price-col-min), 1fr));
    gap: var(--space-md);
  }

  .mini-label {
    display: block;
    margin-bottom: var(--space-2xs);
    color: var(--color-muted);
    font-size: var(--font-size-xs);
    font-weight: 800;
    text-transform: uppercase;
  }
}

/* Mobile landscape */
@media (orientation: landscape) and (max-height: 520px) {
  .protocol-page {
    padding-top: var(--tg-content-safe-area-inset-top, 0px);
    padding-right: var(--tg-content-safe-area-inset-right, 0px);
    padding-bottom: var(--tg-content-safe-area-inset-bottom, 0px);
    padding-left: var(--tg-content-safe-area-inset-left, 0px);
  }

  .protocol-page main {
    max-width: none;
  }

  .panel {
    border-left: 0;
    border-right: 0;
    border-radius: 0;
  }
}

@media (max-width: 760px), (orientation: landscape) and (max-height: 520px) {
  .protocol-page .submission-time-cell {
    display: none;
  }
}

/* Mobile list layout */
@media (max-width: 720px) {
  .list-page {
    padding: var(--space-xl);
  }

  .auth-page {
    padding: var(--space-xl);
  }

  header {
    display: block;
    margin-bottom: var(--space-xl);
  }

  .list-page table {
    background: transparent;
    border: 0;
  }

  .list-page table,
  .list-page thead,
  .list-page tbody,
  .list-page tr,
  .list-page th,
  .list-page td {
    display: block;
  }

  .list-page thead {
    display: none;
  }

  .list-page tr {
    margin-bottom: var(--space-lg);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-surface);
    overflow: hidden;
  }

  .list-page td {
    display: grid;
    grid-template-columns: var(--mobile-label-col-width) minmax(0, 1fr);
    gap: var(--space-lg);
    padding: var(--space-md) var(--space-lg);
    white-space: normal;
  }

  .list-page td::before {
    font-size: var(--font-size-sm);
    font-weight: 700;
  }

  .list-page .button {
    width: 100%;
  }

  .search-bar {
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .pagination {
    display: grid;
    justify-content: stretch;
  }

  .pagination-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}

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