:root {
  --bg: var(--tg-theme-bg-color, #f4f6fa);
  --surface: var(--tg-theme-secondary-bg-color, #ffffff);
  --surface-raised: #ffffff;
  --text: var(--tg-theme-text-color, #162033);
  --muted: var(--tg-theme-hint-color, #7c8799);
  --line: rgba(34, 48, 73, 0.1);
  --primary: var(--tg-theme-button-color, #2563eb);
  --primary-text: var(--tg-theme-button-text-color, #ffffff);
  --blue: #2563eb;
  --green: #0f9f6e;
  --violet: #7c3aed;
  --orange: #e56b17;
  --red: #d64045;
  --shadow: 0 12px 35px rgba(30, 43, 68, 0.08);
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html { background: var(--bg); }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background:
    radial-gradient(circle at 100% -10%, rgba(37, 99, 235, 0.14), transparent 34rem),
    var(--bg);
}

button, input { font: inherit; }
button { color: inherit; }

.hidden { display: none !important; }

.app-shell {
  width: min(100%, 1400px);
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) 16px max(32px, env(safe-area-inset-bottom));
}

.topbar,
.section-heading,
.drawer-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

h1, h2, p { margin: 0; }
h1 { font-size: clamp(28px, 7vw, 42px); letter-spacing: -0.04em; }
h2 { font-size: 22px; letter-spacing: -0.025em; }

.eyebrow {
  margin-bottom: 6px;
  color: var(--primary);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.14em;
}

.muted { color: var(--muted); }
#period { margin-top: 7px; font-size: 13px; }

.icon-button {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 14px;
  color: var(--text);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  box-shadow: var(--shadow);
  cursor: pointer;
  font-size: 24px;
}

.icon-button:active { transform: scale(0.96); }
.icon-button.loading span { display: inline-block; animation: spin 0.9s linear infinite; }

.cabinet-tabs {
  display: flex;
  gap: 9px;
  margin: 22px -16px 18px;
  padding: 0 16px 6px;
  overflow-x: auto;
  scrollbar-width: none;
}
.cabinet-tabs::-webkit-scrollbar { display: none; }

.cabinet-tab {
  flex: 0 0 auto;
  padding: 11px 15px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: var(--surface);
  cursor: pointer;
  white-space: nowrap;
}

.cabinet-tab.active {
  border-color: var(--primary);
  color: var(--primary-text);
  background: var(--primary);
  box-shadow: 0 9px 22px color-mix(in srgb, var(--primary) 25%, transparent);
}

.period-panel {
  margin: -6px 0 18px;
}

.period-picker {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 7px;
}

.period-button {
  min-height: 42px;
  padding: 8px 6px;
  border: 1px solid var(--line);
  border-radius: 13px;
  color: var(--muted);
  background: var(--surface);
  cursor: pointer;
  font-size: 12px;
  font-weight: 700;
}

.period-button.active {
  border-color: color-mix(in srgb, var(--primary) 65%, var(--line));
  color: var(--primary);
  background: color-mix(in srgb, var(--primary) 11%, var(--surface));
}

.custom-period {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 9px;
  margin-top: 10px;
  padding: 13px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.custom-period label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.custom-period input {
  width: 100%;
  min-height: 42px;
  padding: 8px 9px;
  border: 1px solid var(--line);
  border-radius: 11px;
  outline: 0;
  color: var(--text);
  color-scheme: light dark;
  background: var(--bg);
}

.custom-period .primary-button { grid-column: 1 / -1; }

.state-card,
.identity-card,
.table-section {
  border: 1px solid var(--line);
  border-radius: 22px;
  background: color-mix(in srgb, var(--surface) 96%, transparent);
  box-shadow: var(--shadow);
}

.state-card {
  display: grid;
  min-height: 240px;
  padding: 28px;
  place-content: center;
  justify-items: center;
  gap: 16px;
  text-align: center;
  color: var(--muted);
}

.state-icon {
  display: grid;
  width: 54px;
  height: 54px;
  place-items: center;
  border-radius: 17px;
  color: white;
  background: var(--red);
  font-size: 28px;
  font-weight: 800;
}

.loader {
  width: 38px;
  height: 38px;
  border: 4px solid color-mix(in srgb, var(--primary) 18%, transparent);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.primary-button {
  padding: 11px 18px;
  border: 0;
  border-radius: 13px;
  color: var(--primary-text);
  background: var(--primary);
  cursor: pointer;
}

.identity-card {
  padding: 18px 20px;
  border-left: 4px solid var(--primary);
}
.identity-card h2 { margin-bottom: 5px; }
.identity-meta { display: flex; flex-wrap: wrap; gap: 7px; color: var(--muted); font-size: 13px; }

.warnings {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}
.warning {
  padding: 11px 13px;
  border: 1px solid rgba(229, 107, 23, 0.25);
  border-radius: 13px;
  color: color-mix(in srgb, var(--orange) 80%, var(--text));
  background: rgba(229, 107, 23, 0.08);
  font-size: 13px;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 11px;
  margin: 14px 0;
}

.metric-card {
  position: relative;
  min-height: 127px;
  padding: 16px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 19px;
  background: var(--surface);
  box-shadow: 0 7px 24px rgba(30, 43, 68, 0.045);
}

.metric-card::after {
  position: absolute;
  top: -28px;
  right: -28px;
  width: 76px;
  height: 76px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--primary) 9%, transparent);
  content: "";
}
.metric-card.accent-blue { --primary: var(--blue); }
.metric-card.accent-green { --primary: var(--green); }
.metric-card.accent-violet { --primary: var(--violet); }
.metric-card.accent-orange { --primary: var(--orange); }

.metric-label { display: block; min-height: 32px; color: var(--muted); font-size: 12px; line-height: 1.25; }
.metric-card strong { display: block; margin: 8px 0 5px; font-size: clamp(21px, 6vw, 30px); letter-spacing: -0.04em; }
.metric-card small { color: var(--muted); font-size: 11px; }
.metric-card.wide { grid-column: 1 / -1; min-height: 112px; }
.table-section { padding: 18px 0 8px; overflow: hidden; }
.table-section .section-heading { padding: 0 18px; }
.counter {
  min-width: 36px;
  padding: 7px 10px;
  border-radius: 999px;
  color: var(--primary);
  background: color-mix(in srgb, var(--primary) 11%, transparent);
  text-align: center;
  font-weight: 700;
}

.search-box {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 16px 18px;
  padding: 10px 13px;
  border: 1px solid var(--line);
  border-radius: 14px;
  color: var(--muted);
  background: var(--bg);
}
.article-filter-note {
  margin: 7px 18px 0;
  color: var(--muted);
  font-size: 11px;
}
.search-box input {
  width: 100%;
  border: 0;
  outline: 0;
  color: var(--text);
  background: transparent;
}
.search-box input::placeholder { color: var(--muted); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 12px; white-space: nowrap; }
th, td { padding: 12px 14px; border-top: 1px solid var(--line); text-align: right; }
th { color: var(--muted); background: color-mix(in srgb, var(--bg) 70%, transparent); font-size: 10px; font-weight: 700; letter-spacing: 0.02em; }
th:first-child, td:first-child { position: sticky; left: 0; z-index: 1; min-width: 155px; text-align: left; background: var(--surface); }
th:first-child { z-index: 2; background: color-mix(in srgb, var(--bg) 96%, var(--surface)); }
tbody tr { cursor: pointer; }
tbody tr:active td { background: color-mix(in srgb, var(--primary) 7%, var(--surface)); }
.article-code { display: block; max-width: 150px; overflow: hidden; text-overflow: ellipsis; font-weight: 750; }
.article-name { display: block; max-width: 150px; margin-top: 3px; overflow: hidden; color: var(--muted); text-overflow: ellipsis; font-size: 10px; }
.good { color: var(--green); font-weight: 700; }
.attention { color: var(--orange); font-weight: 700; }
.danger { color: var(--red); font-weight: 700; }
.muted-cell { color: var(--muted); }
.empty-message { padding: 30px; color: var(--muted); text-align: center; }

footer { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 18px 4px 0; font-size: 11px; }
.text-button { padding: 0; border: 0; color: var(--primary); background: transparent; cursor: pointer; text-align: right; }

.backdrop {
  position: fixed;
  z-index: 20;
  inset: 0;
  background: rgba(10, 18, 32, 0.45);
  backdrop-filter: blur(3px);
}

.drawer {
  position: fixed;
  z-index: 21;
  right: 0;
  bottom: 0;
  left: 0;
  max-height: 88vh;
  padding: 10px 18px max(24px, env(safe-area-inset-bottom));
  overflow-y: auto;
  border-radius: 24px 24px 0 0;
  background: var(--surface-raised);
  box-shadow: 0 -22px 60px rgba(12, 22, 40, 0.2);
  transform: translateY(105%);
  transition: transform 0.24s ease;
}
.drawer.open { transform: translateY(0); }
.drawer-handle { width: 42px; height: 4px; margin: 0 auto 16px; border-radius: 99px; background: var(--line); }
.drawer-header { position: sticky; top: -10px; z-index: 2; padding: 8px 0 14px; background: var(--surface-raised); }
.drawer-header h2 { max-width: 70vw; overflow-wrap: anywhere; }
.drawer-body { display: grid; gap: 14px; }
.detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; }
.detail-card { padding: 13px; border: 1px solid var(--line); border-radius: 15px; background: var(--bg); }
.detail-card span { display: block; color: var(--muted); font-size: 10px; }
.detail-card strong { display: block; margin-top: 5px; font-size: 18px; }
.cluster-list { display: grid; gap: 8px; }
.cluster-row { padding: 12px; border: 1px solid var(--line); border-radius: 14px; }
.cluster-head { display: flex; justify-content: space-between; gap: 12px; margin-bottom: 8px; font-size: 12px; }
.stock-bar { height: 7px; overflow: hidden; border-radius: 99px; background: var(--line); }
.stock-bar span { display: block; height: 100%; border-radius: inherit; background: var(--green); }
.definitions { display: grid; gap: 10px; }
.definition { padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.definition:last-child { border-bottom: 0; }
.definition strong { display: block; margin-bottom: 4px; }
.definition p { color: var(--muted); font-size: 12px; line-height: 1.45; }
.info-note {
  padding: 12px 14px;
  border: 1px solid color-mix(in srgb, var(--blue) 24%, var(--line));
  border-radius: 14px;
  color: var(--muted);
  background: color-mix(in srgb, var(--blue) 7%, var(--surface));
  font-size: 12px;
  line-height: 1.45;
}

@media (max-width: 759px) {
  .table-section { padding-right: 12px; padding-left: 12px; }
  .table-section .section-heading { padding: 0 6px; }
  .article-filter-note { margin-right: 6px; margin-left: 6px; }
  .search-box { margin-right: 6px; margin-left: 6px; }
  .table-wrap { overflow: visible; }
  table, tbody { display: block; width: 100%; white-space: normal; }
  thead { display: none; }
  tbody { display: grid; gap: 10px; }
  tbody tr {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0 14px;
    padding: 13px;
    border: 1px solid var(--line);
    border-radius: 16px;
    background: var(--surface);
    box-shadow: 0 5px 18px rgba(30, 43, 68, 0.04);
  }
  tbody td,
  tbody td:first-child {
    position: static;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    min-width: 0;
    padding: 9px 0;
    border-top: 1px solid var(--line);
    text-align: left;
    background: transparent;
    overflow-wrap: anywhere;
  }
  tbody td::before {
    display: block;
    margin-bottom: 3px;
    color: var(--muted);
    content: attr(data-label);
    font-size: 9px;
    font-weight: 700;
    letter-spacing: 0.03em;
    text-transform: uppercase;
  }
  tbody td.article-cell {
    grid-column: 1 / -1;
    padding-top: 0;
    border-top: 0;
  }
  tbody td.article-cell::before { display: none; }
  .article-code, .article-name { max-width: 100%; }
}

@media (min-width: 760px) {
  .app-shell { padding-right: 28px; padding-left: 28px; }
  .cabinet-tabs { margin-right: -28px; margin-left: -28px; padding-right: 28px; padding-left: 28px; }
  .metric-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .metric-card.wide { grid-column: auto; }
  .drawer { top: 0; left: auto; width: min(520px, 90vw); max-height: 100vh; border-radius: 24px 0 0 24px; transform: translateX(105%); }
  .drawer.open { transform: translateX(0); }
  table { font-size: 13px; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #0f1728);
    --surface: var(--tg-theme-secondary-bg-color, #141e31);
    --surface-raised: #172033;
    --text: var(--tg-theme-text-color, #f4f7ff);
    --muted: var(--tg-theme-hint-color, #9ca8bc);
    --line: rgba(220, 230, 248, 0.11);
    --shadow: 0 12px 38px rgba(0, 0, 0, 0.22);
  }
}
