:root {
  --bg: #f5f7f9;
  --panel: #ffffff;
  --ink: #18212b;
  --muted: #657282;
  --line: #dce3ea;
  --accent: #126e82;
  --accent-dark: #0c4f5e;
  --warning: #b35b00;
  --danger: #b42318;
  --ok: #087443;
  --soft: #eef6f8;
  --shadow: 0 16px 36px rgba(24, 33, 43, 0.08);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  background: var(--bg);
}

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

button {
  border: 0;
  cursor: pointer;
}

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

.sidebar {
  background: #17232c;
  color: #fff;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

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

.brand-mark {
  width: 44px;
  height: 44px;
  border-radius: 8px;
  background: #d9f2f0;
  color: #12333a;
  display: grid;
  place-items: center;
  font-weight: 800;
}

.brand h1,
.brand p,
.topbar h2,
.topbar p {
  margin: 0;
}

.brand h1 {
  font-size: 18px;
}

.brand p {
  color: #aec0c8;
  font-size: 13px;
}

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

.nav-item {
  color: #dbe7ec;
  text-align: left;
  background: transparent;
  border-radius: 8px;
  padding: 12px;
}

.nav-item.active,
.nav-item:hover {
  background: rgba(255, 255, 255, 0.12);
}

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

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

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

.topbar h2 {
  font-size: 26px;
}

.topbar p {
  margin-top: 6px;
  color: var(--muted);
}

.actions,
.send-actions,
.email-head {
  display: flex;
  gap: 10px;
  align-items: center;
}

.primary,
.secondary,
.file-button {
  min-height: 40px;
  border-radius: 8px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  white-space: nowrap;
}

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

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

.secondary,
.file-button {
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
}

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

.file-button input {
  display: none;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.hidden {
  display: none !important;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.metrics article,
.map-search,
.form-card,
.email-card,
.rule-card,
.detail-panel,
.lead-list {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.metrics article {
  padding: 16px;
}

.map-search {
  padding: 18px;
  margin-bottom: 16px;
}

.map-search-head {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  margin-bottom: 14px;
}

.map-search h3,
.map-search p {
  margin: 0;
}

.map-search p,
.api-panel p,
.fetch-status,
.fetch-note {
  color: var(--muted);
  line-height: 1.6;
}

.fetch-note {
  margin: 0 0 12px;
  font-size: 13px;
}

.map-controls label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.map-controls {
  display: grid;
  grid-template-columns: 180px 240px auto;
  gap: 10px;
  align-items: end;
  margin-bottom: 14px;
}

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

.category-grid label {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  font-size: 13px;
  font-weight: 700;
  background: #fff;
}

.category-grid input {
  width: auto;
}

.cost-strip {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}

.cost-strip span {
  background: #f4f8fa;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 10px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.metrics span {
  color: var(--muted);
  display: block;
  font-size: 13px;
}

.metrics strong {
  font-size: 28px;
  margin-top: 6px;
  display: block;
}

.toolbar {
  display: grid;
  grid-template-columns: minmax(240px, 1fr) 180px 180px 160px;
  gap: 10px;
  margin-bottom: 16px;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 10px 12px;
}

.lead-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 16px;
  align-items: start;
}

.lead-list {
  overflow: hidden;
}

.lead-row {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(260px, 1.8fr) 78px 40px;
  gap: 12px;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  width: 100%;
  text-align: left;
}

.lead-row:last-child {
  border-bottom: 0;
}

.lead-row:hover,
.lead-row.selected {
  background: var(--soft);
}

.lead-title {
  font-weight: 800;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lead-meta,
.small,
.site-url {
  color: var(--muted);
  font-size: 13px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.site-url {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.danger-icon {
  color: var(--danger);
}

.danger-action {
  background: var(--danger);
}

.danger-action:hover {
  background: #8f1d14;
}

.badge {
  border-radius: 999px;
  padding: 5px 9px;
  font-size: 12px;
  font-weight: 800;
  text-align: center;
  background: #edf1f4;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.city-tag {
  display: inline-flex;
  max-width: 120px;
  vertical-align: middle;
  border-radius: 999px;
  background: #eef6f8;
  color: var(--accent-dark);
  padding: 2px 7px;
  font-size: 12px;
  font-weight: 800;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.badge.ok {
  color: var(--ok);
  background: #e8f6ef;
}

.badge.warn {
  color: var(--warning);
  background: #fff4e4;
}

.badge.danger {
  color: var(--danger);
  background: #feeceb;
}

.detail-panel {
  padding: 18px;
  position: sticky;
  top: 20px;
  min-width: 0;
  overflow: hidden;
}

.detail-panel h3 {
  margin: 0 0 4px;
  overflow-wrap: anywhere;
}

.detail-section {
  border-top: 1px solid var(--line);
  margin-top: 16px;
  padding-top: 16px;
  min-width: 0;
}

.detail-section p,
.detail-section a {
  overflow-wrap: anywhere;
  word-break: break-word;
}

.detail-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 16px;
}

.composer-grid {
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr);
  gap: 16px;
  align-items: stretch;
}

.form-card,
.email-card,
.rule-card {
  padding: 18px;
}

.form-card h3,
.rule-card h3 {
  margin: 0 0 16px;
}

.form-card label,
.dialog-grid label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 12px;
}

.form-card .checkbox-label {
  display: flex;
  align-items: center;
  color: var(--ink);
  gap: 8px;
}

.checkbox-label input {
  width: auto;
}

.inline-control {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.sender-list {
  display: grid;
  gap: 8px;
  margin: 0 0 14px;
}

.sender-list-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.row-actions {
  display: inline-flex;
  gap: 8px;
}

.compact-button {
  min-height: 32px;
  padding: 0 10px;
}

.sender-list-row span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 800;
}

.link-row-button {
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--ink);
  text-align: left;
  font-weight: 800;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 0;
}

.full {
  width: 100%;
}

.email-card {
  display: grid;
  grid-template-rows: auto minmax(420px, 1fr) auto;
  gap: 12px;
}

.email-head {
  align-items: end;
}

.email-head > div {
  flex: 1;
}

#emailBody {
  min-height: 420px;
  resize: vertical;
  line-height: 1.7;
}

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

.settings-grid {
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.settings-grid.single {
  grid-template-columns: minmax(320px, 720px);
}

.settings-grid.offer-type-layout {
  grid-template-columns: minmax(260px, 360px) minmax(0, 760px);
}

.rule-card {
  box-shadow: none;
}

.rule-card p,
.rule-card label {
  color: var(--muted);
  line-height: 1.7;
}

.rule-card label {
  display: block;
  margin: 12px 0;
}

.usage-bars {
  display: grid;
  gap: 12px;
  margin-bottom: 16px;
}

.usage-row {
  display: grid;
  gap: 6px;
}

.usage-row-header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.usage-track {
  height: 10px;
  border-radius: 999px;
  background: #e7edf2;
  overflow: hidden;
}

.usage-fill {
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
}

.usage-fill.warn {
  background: var(--warning);
}

.usage-fill.danger {
  background: var(--danger);
}

pre {
  overflow: auto;
  background: #f4f6f8;
  border-radius: 8px;
  padding: 12px;
  line-height: 1.6;
}

dialog {
  border: 0;
  border-radius: 8px;
  padding: 0;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.25);
  width: min(760px, calc(100vw - 32px));
}

dialog::backdrop {
  background: rgba(10, 20, 28, 0.45);
}

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  max-width: min(420px, calc(100vw - 40px));
  padding: 12px 14px;
  border-radius: 8px;
  background: #17232c;
  color: #fff;
  box-shadow: 0 16px 42px rgba(0, 0, 0, 0.22);
  font-size: 14px;
  font-weight: 800;
  line-height: 1.5;
  opacity: 0;
  transform: translateY(10px);
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
  z-index: 20;
}

.toast.active {
  opacity: 1;
  transform: translateY(0);
}

.toast.error {
  background: var(--danger);
}

.toast.success {
  background: var(--ok);
}

#leadForm {
  padding: 22px;
}

.confirm-dialog {
  width: min(420px, calc(100vw - 32px));
}

.confirm-content {
  padding: 22px;
}

.confirm-content h3 {
  margin: 0 0 10px;
}

.confirm-content p {
  color: var(--muted);
  line-height: 1.7;
}

#leadForm h3 {
  margin: 0;
}

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

.icon-button {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
}

.dialog-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

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

.dialog-grid textarea {
  min-height: 96px;
}

menu {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 0;
  margin: 18px 0 0;
}

@media (max-width: 980px) {
  .app-shell,
  .lead-layout,
  .composer-grid,
  .rules-grid,
  .settings-grid {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
  }

  .metrics,
  .toolbar,
  .map-controls,
  .category-grid,
  .cost-strip {
    grid-template-columns: 1fr 1fr;
  }

  .lead-row {
    grid-template-columns: 1fr 40px;
  }

  .lead-row > :nth-child(2),
  .lead-row > :nth-child(3) {
    display: none;
  }
}

@media (max-width: 640px) {
  .main {
    padding: 18px;
  }

  .topbar,
  .actions,
  .send-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .metrics,
  .toolbar,
  .map-controls,
  .category-grid,
  .cost-strip,
  .dialog-grid {
    grid-template-columns: 1fr;
  }
}
