:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --line: #d7dde4;
  --text: #18212f;
  --muted: #667085;
  --accent: #0f766e;
  --accent-dark: #0b5f59;
  --danger: #b42318;
  --soft: #e9f5f3;
  --header: #f8fafc;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  background: #f7f9fb;
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
}

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

button {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 6px;
  padding: 9px 12px;
  cursor: pointer;
  white-space: nowrap;
}

button:hover {
  border-color: var(--accent);
}

button.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

button.primary:hover {
  background: var(--accent-dark);
}

button.danger {
  color: var(--danger);
  border-color: #f2b8b5;
}

input,
select,
textarea {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 10px;
  background: #fff;
  color: var(--text);
}

textarea {
  resize: vertical;
}

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

.sidebar {
  border-right: 1px solid var(--line);
  background: #ffffff;
  padding: 12px;
  overflow: auto;
}

.brand {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 20px;
}

.compact-brand {
  margin-bottom: 14px;
}

.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 18px;
}

h2 {
  font-size: 16px;
}

h3 {
  margin: 0 0 10px;
  font-size: 14px;
}

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

.panel {
  border: 0;
  border-top: 1px solid var(--line);
  border-radius: 0;
  padding: 14px 0 0;
  margin-bottom: 12px;
}

.region-panel {
  max-height: calc(100vh - 120px);
  overflow: hidden;
  display: grid;
}

.compact-panel {
  display: grid;
  gap: 8px;
}

.compact-panel button {
  width: 100%;
}

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

.panel label {
  display: grid;
  gap: 6px;
  margin-bottom: 10px;
}

.panel label span,
.field span {
  color: var(--muted);
  font-size: 13px;
}

.inline-check {
  display: flex !important;
  grid-template-columns: none !important;
  align-items: center;
  gap: 6px !important;
  margin: 0 !important;
}

.inline-check input {
  width: 16px;
  height: 16px;
}

.preview {
  margin-top: 12px;
  padding: 10px;
  min-height: 84px;
  max-height: 120px;
  border-radius: 6px;
  background: var(--soft);
  font-family: Consolas, monospace;
  white-space: pre-wrap;
  word-break: break-word;
  overflow: hidden;
}

.side-actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.region-list {
  max-height: min(620px, calc(100vh - 260px));
  overflow: auto;
  display: grid;
  gap: 8px;
  padding-right: 4px;
}

.region-item {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr) auto;
  gap: 8px;
  align-items: start;
  padding: 8px 0;
  border: 0;
  border-bottom: 1px solid #edf1f5;
  border-radius: 6px;
}

.region-check {
  width: 16px;
  height: 16px;
  margin-top: 10px;
}

.region-main {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.region-main strong {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.region-main small {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.region-edit {
  display: grid;
  gap: 6px;
  margin-top: 6px;
}

.region-actions {
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 120ms ease;
}

.region-item:hover .region-actions,
.region-item:focus-within .region-actions {
  opacity: 1;
}

.region-form {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.region-add {
  margin-top: 10px;
}

.region-add summary {
  cursor: pointer;
  color: var(--accent-dark);
  font-weight: 700;
}

.regions-modal-body {
  padding: 14px 16px 16px;
}

.region-modal-toolbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  margin-bottom: 10px;
}

.backup-list {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}

.backup-details summary {
  cursor: pointer;
  color: var(--accent-dark);
  font-weight: 700;
}

.backup-link {
  display: grid;
  gap: 2px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  text-decoration: none;
}

.backup-link:hover {
  border-color: var(--accent);
}

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

.topbar {
  display: grid;
  grid-template-columns: 180px minmax(0, 1fr);
  gap: 12px;
  align-items: center;
  margin-bottom: 12px;
}

.topbar-title {
  min-width: 160px;
}

.tools {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) repeat(6, auto);
  gap: 8px;
  align-items: center;
}

.selection-bar {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 10px;
  padding: 8px 10px;
  border: 1px solid #b7d8d4;
  border-radius: 8px;
  background: var(--soft);
}

.selection-bar span {
  margin-right: auto;
  color: var(--accent-dark);
  font-weight: 700;
}

.message {
  margin-bottom: 12px;
  border-radius: 6px;
  padding: 10px 12px;
  background: var(--soft);
  color: var(--accent-dark);
}

.message.error {
  background: #fef3f2;
  color: var(--danger);
}

.table-wrap {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  overflow: auto;
  max-height: calc(100vh - 86px);
}

table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  min-width: 0;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 8px 10px;
  text-align: left;
  vertical-align: top;
  font-size: 14px;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--header);
  color: #344054;
  font-weight: 700;
}

tr.selected td {
  background: var(--soft);
}

.check-col {
  width: 44px;
  text-align: center;
}

th.check-col,
td.check-col {
  width: 42px;
}

.check-col input {
  width: 16px;
  height: 16px;
}

.title-cell {
  font-weight: 700;
  line-height: 1.25;
}

.title-link {
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--text);
  font-weight: 700;
  text-align: left;
  white-space: normal;
}

.title-link:hover {
  color: var(--accent-dark);
  text-decoration: underline;
}

th:nth-child(2),
td:nth-child(2) {
  width: 5%;
}

th:nth-child(3),
td:nth-child(3) {
  width: 31%;
}

th:nth-child(4),
td:nth-child(4) {
  width: 34%;
}

th:nth-child(5),
td:nth-child(5) {
  width: 8%;
}

th:nth-child(6),
td:nth-child(6) {
  width: 22%;
}

.manual-cell {
  max-height: 116px;
  font-family: Consolas, monospace;
  white-space: pre-wrap;
  word-break: break-word;
  overflow: hidden;
  position: relative;
}

.manual-cell.expanded {
  max-height: none;
  overflow: visible;
}

.manual-more {
  display: inline-block;
  margin-top: 4px;
  padding: 2px 7px;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 12px;
  color: var(--accent-dark);
  background: var(--soft);
  border-color: #b7d8d4;
}

.row-actions {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  align-items: center;
}

.row-actions button {
  padding: 7px 10px;
}

dialog {
  width: min(1180px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
}

dialog::backdrop {
  background: rgb(24 33 47 / 45%);
}

#itemForm {
  display: grid;
  max-height: calc(100vh - 32px);
}

#massForm {
  display: grid;
  max-height: calc(100vh - 32px);
}

.dialog-head,
.dialog-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.dialog-actions {
  border-top: 1px solid var(--line);
  border-bottom: 0;
  justify-content: flex-end;
}

.dialog-actions .danger {
  margin-right: auto;
}

.duplicates-body {
  display: grid;
  gap: 10px;
  padding: 12px 16px 16px;
  max-height: calc(100vh - 120px);
  overflow: auto;
}

.duplicate-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  position: sticky;
  top: -12px;
  z-index: 2;
  padding: 8px 0;
  background: var(--surface);
}

.duplicate-mini-stats {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 5px 8px;
  color: var(--muted);
  font-size: 13px;
}

.duplicate-mini-stats b {
  color: var(--text);
  font-size: 18px;
}

.duplicate-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 6px;
}

.duplicate-actions > button {
  padding: 7px 10px;
}

.duplicate-actions input {
  width: 86px;
  padding: 7px 8px;
}

.duplicate-actions .duplicate-search {
  width: 260px;
}

.segmented {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 7px;
  overflow: hidden;
  background: #fff;
}

.segmented button {
  border: 0;
  border-radius: 0;
  padding: 7px 10px;
  background: #fff;
}

.segmented button + button {
  border-left: 1px solid var(--line);
}

.segmented button.active {
  background: var(--accent);
  color: #fff;
}

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

.unique-ads-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: auto;
}

.unique-ads-table th,
.unique-ads-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
  text-align: left;
}

.unique-ads-table th {
  background: var(--header);
  color: #344054;
  font-size: 12px;
  position: sticky;
  top: 0;
  z-index: 1;
}

.unique-ads-table td {
  font-size: 13px;
  line-height: 1.2;
}

.unique-ads-table .check-col {
  width: 34px;
  text-align: center;
}

.unique-ads-table .offer-cell {
  width: 34%;
  min-width: 260px;
}

.unique-ads-table .count-cell {
  width: 74px;
}

.unique-ads-table .actions-cell {
  width: 76px;
  text-align: right;
}

.unique-ads-table small,
.unique-ads-table .count-cell span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
}

.unique-ads-table tr.has-duplicates {
  background: #f8fffe;
}

.unique-ads-table .check-col input {
  width: 16px;
  height: 16px;
}

.details-toggle {
  padding: 5px 8px;
  font-size: 12px;
}

.details-row td {
  background: #fbfcfd;
  padding: 0;
}

.offer-detail-head,
.offer-detail {
  display: grid;
  grid-template-columns: 58px minmax(180px, 1fr) minmax(150px, 0.6fr) minmax(220px, 1.2fr);
  gap: 8px;
  padding: 7px 10px;
  border-top: 1px solid var(--line);
}

.offer-detail-head {
  color: var(--muted);
  font-size: 12px;
  background: #f4f6f8;
}

.offer-detail {
  font-size: 12px;
}

.fields-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  padding: 16px;
  overflow: auto;
}

.mass-body {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  padding: 16px;
  overflow: auto;
}

.mass-section {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
}

.mass-section:first-child {
  grid-column: 1 / -1;
}

.mass-source-list {
  display: grid;
  gap: 6px;
  max-height: 260px;
  overflow: auto;
  margin-top: 8px;
}

.mass-source-row {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr);
  gap: 8px;
  align-items: start;
  padding: 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.mass-source-row small {
  grid-column: 2;
}

.mass-source-row input {
  width: 16px;
  height: 16px;
}

.add-line {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto;
  gap: 8px;
  margin-bottom: 8px;
}

.city-picker {
  position: relative;
  margin-bottom: 8px;
}

.city-picker > button {
  width: 100%;
  text-align: left;
}

.city-menu {
  position: absolute;
  z-index: 10;
  top: calc(100% + 6px);
  left: 0;
  width: min(520px, 92vw);
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 16px 36px rgb(24 33 47 / 18%);
}

.city-menu-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 8px 0;
}

.city-options {
  display: grid;
  gap: 4px;
  max-height: 320px;
  overflow: auto;
}

.city-option {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr);
  gap: 8px;
  align-items: center;
  padding: 7px;
  border-radius: 6px;
}

.city-option:hover {
  background: var(--soft);
}

.city-option input {
  width: 16px;
  height: 16px;
}

.mass-regions {
  display: grid;
  gap: 8px;
  max-height: 280px;
  overflow: auto;
}

.mass-region {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr) 92px 110px;
  gap: 8px;
  align-items: center;
}

.mass-region input[type="checkbox"] {
  width: 16px;
  height: 16px;
}

.manual-builder {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px;
}

.manual-builder-toolbar {
  display: grid;
  grid-template-columns: 120px 140px auto;
  gap: 8px;
  margin-bottom: 8px;
}

.manual-region-list {
  display: grid;
  gap: 6px;
  max-height: 280px;
  overflow: auto;
  margin-bottom: 8px;
}

.manual-region-row {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr) 90px 110px;
  gap: 8px;
  align-items: center;
}

.manual-region-row input[type="checkbox"] {
  width: 16px;
  height: 16px;
}

.manual-raw {
  font-family: Consolas, monospace;
}

.bulk-edit-body,
.selected-rates-body {
  display: grid;
  gap: 12px;
  padding: 16px;
}

.selected-rates-body {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.selected-rates-body .muted {
  grid-column: 1 / -1;
  margin: 0;
}

.field {
  display: grid;
  gap: 6px;
}

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

.field small {
  min-height: 14px;
}

.html-editor {
  min-height: 260px;
  max-height: 480px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px;
  background: #fff;
  line-height: 1.45;
}

.html-editor:focus {
  outline: 2px solid #b7d8d4;
  border-color: var(--accent);
}

.html-editor p {
  margin: 0 0 10px;
}

.html-editor ul {
  margin: 8px 0 8px 22px;
  padding: 0;
}

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

  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .toolbar,
  .topbar,
  .tools,
  .fields-grid,
  .selected-rates-body,
  .mass-body,
  .duplicate-toolbar,
  .offer-detail-head,
  .offer-detail {
    display: grid;
    grid-template-columns: 1fr;
  }

  .mass-section:first-child {
    grid-column: auto;
  }
}
