@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');

:root {
  --ink: #17231f;
  --muted: #65736d;
  --paper: #f4f6f2;
  --surface: #ffffff;
  --soft: #eef3ed;
  --line: #dbe4dd;
  --line-strong: #c9d7cd;
  --green: #276653;
  --green-dark: #173b35;
  --mint: #b6e3c2;
  --orange: #e66d45;
  --red: #b84435;
  --red-soft: #fbe1da;
  --shadow: 0 18px 50px rgba(23, 35, 31, .08);
}

* { box-sizing: border-box; }

html { min-width: 320px; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 14px/1.5 Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

a { color: var(--green); text-decoration-thickness: 1px; text-underline-offset: 3px; }

aside {
  position: fixed;
  inset: 0 auto 0 0;
  width: 248px;
  overflow-y: auto;
  background: var(--green-dark);
  color: #fff;
  padding: 30px 22px;
}

aside strong {
  display: block;
  font-size: 25px;
  font-weight: 800;
  letter-spacing: -.03em;
}

nav {
  display: grid;
  gap: 6px;
  margin-top: 42px;
}

nav a {
  color: #d6e5dd;
  border-radius: 6px;
  padding: 11px 12px;
  text-decoration: none;
}

nav a:hover,
nav a:focus-visible {
  background: rgba(255, 255, 255, .11);
  color: #fff;
  outline: 0;
}

main {
  min-height: 100vh;
  margin-left: 248px;
}

header {
  min-height: 68px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 20px;
  padding: 0 42px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  background: rgba(255, 255, 255, .55);
  backdrop-filter: blur(10px);
}

header a { color: var(--ink); }

.page,
.form-page {
  width: min(100%, 1280px);
  padding: 42px;
}

.form-page { max-width: 900px; }
.form-page.wide,
.campaign-editor-page { max-width: 1180px; }

.page-title {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 18px;
  margin-bottom: 28px;
}

.eyebrow {
  display: block;
  color: var(--orange);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .14em;
}

h1,
h2 {
  margin: 4px 0 0;
  line-height: 1.15;
  letter-spacing: 0;
}

h1 { font-size: clamp(28px, 3vw, 40px); }
h2 { font-size: 20px; }

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

.button,
button {
  display: inline-flex;
  min-height: 42px;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 0;
  border-radius: 6px;
  background: var(--orange);
  color: #fff;
  padding: 10px 16px;
  font: inherit;
  font-weight: 700;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
}

button:hover,
.button:hover { filter: brightness(.96); }

button:focus-visible,
.button:focus-visible,
input:focus,
select:focus,
textarea:focus {
  outline: 3px solid rgba(182, 227, 194, .75);
  outline-offset: 2px;
}

.button.secondary,
button.secondary { background: var(--green); }

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
}

.stat,
.chart,
.table-wrap,
.segment-builder,
.segment-results,
.import-panel,
.editor-shell,
.auth {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.stat { min-width: 0; padding: 20px; }
.stat span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
}
.stat strong {
  display: block;
  margin-top: 10px;
  font-size: 30px;
  line-height: 1;
  overflow-wrap: anywhere;
}

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  min-width: 680px;
  border-collapse: collapse;
  text-align: left;
}

th,
td {
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
  white-space: nowrap;
}

tr:last-child td { border-bottom: 0; }
th {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
}

td a { margin-right: 10px; }
.danger { color: var(--red) !important; }

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  border-radius: 999px;
  background: #e7eee9;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 700;
}
.badge.good { color: #1f684e; background: #d9f0df; }
.badge.bad { color: var(--red); background: var(--red-soft); }

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  padding: 12px;
  font: inherit;
}

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

form { min-width: 0; }
.form-page form { display: grid; gap: 16px; }
.form-grid,
.campaign-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.toolbar,
.inline-form,
.editor-actions,
.segment-actions,
.export-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.toolbar { margin-top: 20px; }
.toolbar input { max-width: 320px; }

.inline-form {
  width: min(100%, 1280px);
  padding: 0 42px 42px;
}

.inline-form form {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(220px, 2fr) auto;
  gap: 10px;
  align-items: end;
  padding: 22px !important;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.auth {
  width: min(calc(100% - 32px), 420px);
  margin: 10vh auto;
  padding: 34px;
}

.auth form {
  display: grid;
  gap: 16px;
  margin: 26px 0 16px;
}

.error,
.notice {
  border-radius: 6px;
  padding: 13px 14px;
}

.error { background: var(--red-soft); color: #97392f; }
.notice { background: #d9f0df; color: #1f684e; }
.auth .notice { margin: 18px 0 0; }

.preview {
  min-height: 120px;
  overflow: auto;
  background: #fff;
  border: 1px dashed var(--line-strong);
  border-radius: 6px;
  padding: 22px;
}

.chart {
  margin-top: 28px;
  padding: 24px;
}

.bars {
  height: 170px;
  display: flex;
  align-items: end;
  gap: 26px;
  margin-top: 18px;
  border-bottom: 1px solid var(--line);
}

.bars i {
  display: block;
  width: 64px;
  min-height: 5px;
  border-radius: 5px 5px 0 0;
  background: var(--mint);
}

.editor-tabs {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--soft);
}

.editor-tab {
  min-height: 38px;
  background: transparent;
  color: var(--muted);
  padding: 8px 13px;
}

.editor-tab.active {
  background: #fff;
  color: var(--ink);
  box-shadow: 0 1px 5px rgba(23, 59, 53, .14);
}

.editor-status {
  margin-left: auto;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.editor-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
}

.editor-toolbar button {
  min-height: 36px;
  background: var(--soft);
  color: var(--ink);
  padding: 8px 11px;
  font-size: 12px;
}

.editor-toolbar button:hover { background: var(--mint); }

.editor-workspace {
  min-width: 0;
  padding: 18px;
  background: #edf2ed;
}

.visual-editor,
.html-editor {
  width: 100%;
  min-height: 340px;
  max-height: 70vh;
  overflow: auto;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: #fff;
  padding: 24px;
  line-height: 1.6;
}

.visual-editor img,
.campaign-preview img {
  max-width: 100%;
  height: auto;
}

.html-editor {
  display: none;
  resize: vertical;
  font: 13px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.html-editor.visible { display: block; }
.visual-editor.hidden,
.editor-toolbar.hidden { display: none; }

.editor-preview-label {
  margin: 24px 0 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.campaign-preview {
  width: min(100%, 720px);
  min-height: 180px;
}

.import-panel {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 360px);
  gap: 22px;
  margin-top: 24px;
  padding: 24px;
}

.import-panel h2 { margin-top: 0; }
.import-panel p { color: var(--muted); line-height: 1.6; }
.import-panel code,
.form-page code,
pre {
  border-radius: 5px;
  background: #17231f;
  color: #d9f0df;
}
.import-panel code {
  display: inline;
  padding: 2px 5px;
  background: var(--soft);
  color: var(--ink);
}
.import-panel form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  justify-content: center;
}
.export-actions { grid-column: 1 / -1; }

.segment-builder {
  max-width: 980px;
  padding: 24px;
}
.segment-controls {
  display: flex;
  align-items: end;
  gap: 20px;
  padding: 18px 0;
  color: var(--muted);
}
.segment-controls label { min-width: 220px; }
.segment-rule {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) minmax(150px, 1fr) minmax(180px, 1.4fr) 42px;
  gap: 10px;
  margin-bottom: 10px;
}
.segment-rule .remove-rule {
  width: 42px;
  min-height: 42px;
  padding: 0;
  background: var(--red-soft);
  color: var(--red);
}
.segment-actions { margin-top: 22px; }
.segment-results {
  margin-top: 28px;
  padding: 22px;
}
.segment-result-list { color: var(--muted); }

.form-page code,
pre {
  display: block;
  overflow: auto;
  padding: 18px;
}

@media (max-width: 900px) {
  aside {
    position: static;
    width: auto;
    height: auto;
    padding: 20px;
  }

  nav {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    margin-top: 18px;
    padding-bottom: 4px;
  }

  nav a { flex: 0 0 auto; }
  main { margin-left: 0; }
  header { padding: 0 20px; }

  .page,
  .form-page,
  .inline-form {
    padding: 28px 18px;
  }

  .page-title {
    align-items: flex-start;
    flex-direction: column;
  }

  .form-grid,
  .campaign-fields,
  .import-panel,
  .segment-rule {
    grid-template-columns: 1fr;
  }

  .toolbar,
  .inline-form,
  .editor-actions,
  .segment-actions,
  .export-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .toolbar input,
  .toolbar .button,
  .toolbar button,
  .editor-actions .button,
  .editor-actions button,
  .segment-actions .button,
  .segment-actions button,
  .export-actions .button {
    width: 100%;
    max-width: none;
  }

  .inline-form form {
    grid-template-columns: 1fr;
    padding: 18px !important;
  }

  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .segment-controls {
    align-items: stretch;
    flex-direction: column;
    gap: 10px;
  }
  .segment-controls label { min-width: 0; }
  .segment-rule .remove-rule { width: 100%; }
  .editor-status { display: none; }
  .editor-workspace { padding: 10px; }
  .visual-editor,
  .html-editor {
    min-height: 280px;
    padding: 18px;
  }
}

@media (max-width: 520px) {
  .stats { grid-template-columns: 1fr; }
  .auth { margin: 5vh auto; padding: 24px; }
  header {
    justify-content: space-between;
    gap: 12px;
  }
}
