:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #18212f;
  --muted: #6b7280;
  --line: #dfe4ea;
  --primary: #176b5b;
  --primary-strong: #0d4f44;
  --accent: #b42318;
  --safe: #137a47;
  --warn: #b54708;
  --shadow: 0 12px 32px rgba(24, 33, 47, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 0;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
}

.sidebar {
  background: #101820;
  color: #ecf2f0;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-row span,
.user-box span {
  display: block;
  color: #9fb2ad;
  font-size: 12px;
  margin-top: 2px;
}

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: #31a184;
  color: #fff;
  font-weight: 800;
}

nav {
  display: grid;
  gap: 8px;
}

.nav-item,
.ghost,
.secondary,
.primary {
  cursor: pointer;
  border-radius: 8px;
  min-height: 38px;
  padding: 0 14px;
  transition: 0.16s ease;
}

.nav-item {
  text-align: left;
  color: #d8e2df;
  background: transparent;
}

.nav-item:hover,
.nav-item.active {
  background: #1e2d34;
  color: #fff;
}

.sidebar-footer {
  margin-top: auto;
  display: grid;
  gap: 12px;
}

.ghost {
  color: #d8e2df;
  border: 1px solid #33434b;
  background: transparent;
}

.ghost:hover {
  background: #1e2d34;
}

.wide {
  width: 100%;
}

.main {
  min-width: 0;
  padding: 24px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.15;
}

h2 {
  font-size: 16px;
}

p {
  color: var(--muted);
  margin: 6px 0 0;
}

.primary {
  background: var(--primary);
  color: white;
  font-weight: 700;
}

.primary:hover {
  background: var(--primary-strong);
}

.secondary {
  background: #e7efec;
  color: var(--primary-strong);
  font-weight: 700;
}

.danger-btn {
  background: #fee4e2;
  color: var(--accent);
}

.view {
  display: none;
}

.view.active {
  display: block;
}

#importAllBtn,
#autoSyncRunBtn,
label:has(#storeImportEnabled) {
  display: none !important;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(130px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.stat-card,
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.stat-card {
  padding: 16px;
}

.stat-card span {
  color: var(--muted);
  font-size: 12px;
}

.stat-card strong {
  display: block;
  font-size: 24px;
  margin-top: 8px;
}

.split-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(340px, 0.65fr);
  gap: 16px;
}

.risk-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 16px;
}

.risk-grid .panel:first-child {
  grid-column: 1 / -1;
}

.panel {
  padding: 16px;
  min-width: 0;
}

.panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.panel-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.muted {
  color: var(--muted);
  font-size: 12px;
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.table-wrap.small {
  max-height: 420px;
}

.mobile-card-list {
  display: none;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 1180px;
  background: #fff;
}

th,
td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

th {
  color: #4b5563;
  background: #f9fafb;
  font-size: 12px;
  font-weight: 800;
}

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

tbody tr {
  cursor: default;
}

tbody tr:hover {
  background: #f8fbfa;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
}

.pill.danger {
  color: var(--accent);
  background: #fee4e2;
}

.pill.safe {
  color: var(--safe);
  background: #dcfae6;
}

.pill.skipped {
  color: #475467;
  background: #eef2f6;
}

.pill.error {
  color: var(--warn);
  background: #fef0c7;
}

.form-stack {
  display: grid;
  gap: 12px;
}

.form-stack.compact {
  gap: 10px;
}

label {
  display: grid;
  gap: 6px;
  color: #4b5563;
  font-size: 12px;
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  min-height: 38px;
  border: 1px solid #cfd7df;
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 8px 10px;
  outline: none;
}

textarea {
  resize: vertical;
  min-height: 72px;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(23, 107, 91, 0.12);
}

.inline-form,
.settings-grid,
.store-form,
.sync-form,
.risk-form {
  display: grid;
  gap: 10px;
  margin-bottom: 14px;
}

.inline-form {
  grid-template-columns: repeat(3, minmax(0, 1fr)) 88px;
}

#userForm {
  grid-template-columns: minmax(180px, 1fr) minmax(180px, 1fr) minmax(180px, 1fr) 110px 140px;
  align-items: end;
}

.settings-grid {
  grid-template-columns: repeat(2, minmax(220px, 1fr));
}

.settings-wide {
  grid-column: 1 / -1;
}

.settings-section {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: #fbfcfd;
}

.settings-section h3 {
  margin: 0;
  font-size: 15px;
}

.settings-section summary {
  cursor: pointer;
  font-weight: 800;
}

.settings-section .inner {
  margin: 12px 0 0;
}

.user-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.user-actions button {
  min-height: 34px;
  padding: 0 10px;
}

.telegram-tools {
  display: flex;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 10px;
  margin: 4px 0 14px;
}

.telegram-chats {
  flex-basis: 100%;
  display: grid;
  gap: 8px;
}

.telegram-chat {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #f8fafc;
}

.store-form {
  grid-template-columns: 1fr 1.35fr 1fr 1fr 1fr 92px 110px 120px 90px;
  align-items: end;
}

.hidden {
  display: none !important;
}

#storesView > .sync-panel {
  display: none !important;
}

.sync-form {
  grid-template-columns: minmax(220px, 1.2fr) repeat(2, minmax(190px, 1fr)) minmax(150px, 0.8fr) minmax(110px, 0.5fr) 130px;
  align-items: end;
}

.store-sync-row td {
  background: #f8fafc;
  padding: 10px 12px;
}

.store-sync-box {
  display: grid;
  grid-template-columns: minmax(160px, 0.7fr) repeat(3, minmax(190px, 1fr)) 96px;
  align-items: end;
  gap: 10px;
}

.store-sync-box strong {
  align-self: center;
}

.risk-form {
  grid-template-columns: minmax(260px, 1.4fr) minmax(120px, 0.5fr) minmax(220px, 0.8fr) minmax(240px, 1fr) 110px;
}

.risk-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: -2px 0 14px;
}

.store-form label:has(input[type="checkbox"]) {
  min-height: 64px;
  align-content: end;
}

.store-form input[type="checkbox"] {
  width: 18px;
  min-height: 18px;
}

.actions-cell {
  white-space: nowrap;
}

.order-link {
  padding: 0;
  min-height: 0;
  color: var(--primary);
  background: transparent;
  font-weight: 800;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.product-summary {
  max-width: 420px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 700;
}

.item-details {
  display: none;
  margin-top: 10px;
}

tr.expanded .item-details {
  display: block;
}

.order-item + .order-item {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

.item-name {
  font-weight: 700;
  line-height: 1.35;
}

.item-options {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}

.item-options span {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  min-height: 24px;
  padding: 2px 8px;
  border-radius: 6px;
  background: #eef6f3;
  color: #123f36;
  font-size: 12px;
}

.result-box,
.notice,
.form-message {
  min-height: 20px;
  color: var(--muted);
}

.result-box {
  border-radius: 8px;
  background: #f8fafc;
  border: 1px solid var(--line);
  padding: 10px;
}

.manual-result {
  display: grid;
  gap: 7px;
  color: var(--text);
}

.manual-result-head {
  display: flex;
  align-items: center;
  gap: 8px;
}

.manual-result.danger {
  color: #7a1f18;
}

.manual-result.safe {
  color: #166534;
}

.manual-result.skipped,
.manual-result.error {
  color: var(--text);
}

.json-box {
  min-height: 180px;
  overflow: auto;
  margin: 14px 0 0;
  padding: 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #0f172a;
  color: #d1e7ff;
  white-space: pre-wrap;
}

.login-body {
  min-height: 100vh;
  background:
    linear-gradient(135deg, rgba(23, 107, 91, 0.18), transparent 38%),
    #eef3f1;
}

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(420px, 100%);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 28px;
}

.login-panel h1 {
  margin-top: 18px;
}

.login-panel form {
  margin-top: 22px;
}

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

  .sidebar {
    position: static;
    padding: 12px 14px;
    gap: 12px;
  }

  .brand-row {
    justify-content: space-between;
  }

  .brand-mark {
    width: 36px;
    height: 36px;
  }

  nav {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding-bottom: 2px;
    scrollbar-width: thin;
  }

  .nav-item {
    flex: 0 0 auto;
    min-height: 36px;
    white-space: nowrap;
  }

  .sidebar-footer {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    margin-top: 0;
  }

  .stats-grid,
  .split-grid,
  .risk-grid,
  .settings-grid,
  .store-form,
  .sync-form,
  .risk-form {
    grid-template-columns: 1fr;
  }

  .risk-toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .table-wrap {
    max-width: calc(100vw - 32px);
  }
}

@media (max-width: 720px) {
  html,
  body {
    max-width: 100%;
    overflow-x: hidden;
  }

  body {
    font-size: 13px;
  }

  .main {
    padding: 10px;
  }

  .sidebar {
    border-radius: 0;
    padding: 12px;
  }

  .sidebar-footer {
    gap: 8px;
  }

  .topbar,
  .panel-head {
    align-items: flex-start;
    flex-direction: column;
    gap: 8px;
  }

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

  h1 {
    font-size: 24px;
  }

  .stats-grid {
    gap: 8px;
  }

  .stat-card,
  .panel {
    padding: 12px;
  }

  .table-wrap {
    max-width: calc(100vw - 24px);
  }

  #ordersView .table-wrap {
    display: none;
  }

  #storesView .table-wrap {
    display: none;
  }

  #ordersView .mobile-card-list {
    display: grid;
    gap: 10px;
  }

  #storesView .mobile-card-list {
    display: grid;
    gap: 10px;
  }

  .mobile-card {
    display: grid;
    gap: 8px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
  }

  .mobile-card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
  }

  .mobile-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }

  .mobile-actions button {
    flex: 1 1 auto;
  }

  .mobile-inline-sync {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--line);
  }

  table {
    min-width: 860px;
  }

  .actions-cell {
    white-space: normal;
  }

  .actions-cell button {
    margin: 2px 0;
  }

  .store-sync-box {
    grid-template-columns: 1fr;
  }

  .form-stack.compact {
    gap: 8px;
  }

  textarea {
    min-height: 88px;
  }

  .login-panel {
    padding: 22px;
  }
}
