:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e1e4ea;
  --text: #1c2130;
  --muted: #6b7385;
  --accent: #e8590c;
  --accent-text: #ffffff;
  --ok: #2b8a3e;
  --ok-bg: #e6f4ea;
  --bad: #c92a2a;
  --bad-bg: #fdeaea;
  --warn: #b35c00;
  --warn-bg: #fff1dc;
  --new: #1864ab;
  --new-bg: #e5f0fb;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(20, 25, 40, .06), 0 1px 1px rgba(20, 25, 40, .04);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151c;
    --surface: #1b1f29;
    --surface-2: #232836;
    --border: #2e3444;
    --text: #e6e8ee;
    --muted: #9198aa;
    --accent: #ff7a33;
    --accent-text: #1b1f29;
    --ok: #69db7c;
    --ok-bg: #1b3324;
    --bad: #ff8787;
    --bad-bg: #3a1f22;
    --warn: #ffc078;
    --warn-bg: #3a2c17;
    --new: #74c0fc;
    --new-bg: #172b40;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; }
h1 { font-size: 1.5rem; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; display: flex; align-items: center; gap: .4rem; }
code, .mono { font-family: ui-monospace, "SF Mono", "Ubuntu Mono", Menlo, monospace; font-size: .9em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.error { color: var(--bad); background: var(--bad-bg); padding: .6rem .8rem; border-radius: 8px; }
.ok-text { color: var(--ok); }
.bad-text { color: var(--bad); }
.warn-text { color: var(--warn); }

/* ---- layout ---- */
.shell { min-height: 100vh; }
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: .6rem 16px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; text-decoration: none; color: var(--text); }
.brand svg { width: 26px; height: 26px; color: var(--accent); }
.userbox { display: flex; align-items: center; gap: .5rem; min-width: 0; }
.userbox img { width: 26px; height: 26px; border-radius: 50%; }
.userbox span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 40vw; font-size: .9rem; }
.content { max-width: 960px; margin: 0 auto; padding: 20px 16px 64px; }
.pagehead { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; }
.back { display: inline-block; margin-bottom: .5rem; color: var(--muted); text-decoration: none; font-size: .9rem; }
.back:hover { color: var(--text); }
.status-line { display: flex; align-items: center; gap: .45rem; color: var(--muted); font-size: .9rem; margin-top: .25rem; flex-wrap: wrap; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}
.empty { text-align: center; padding: 28px 16px; }
.empty p { margin: .25rem 0; }

/* ---- controls ---- */
button, .button {
  font: inherit; font-weight: 550;
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  padding: .55rem 1rem;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: background .12s, border-color .12s;
}
button:hover, .button:hover { background: var(--surface-2); }
button:disabled { opacity: .55; cursor: default; }
button:focus-visible, .button:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.primary:hover { background: color-mix(in srgb, var(--accent) 88%, black); }
button.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, var(--border)); }
button.ok:hover { background: var(--ok-bg); }
button.bad { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, var(--border)); }
button.bad:hover { background: var(--bad-bg); }
.ghost { background: transparent; border-color: transparent; }
button.small, .button.small, select.small { padding: .35rem .7rem; font-size: .85rem; border-radius: 8px; }

input, select {
  font: inherit; color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: .55rem .75rem;
  width: 100%;
  min-width: 0;
}
select { width: auto; }
input[type="checkbox"] { width: auto; accent-color: var(--accent); }
label { display: flex; flex-direction: column; gap: .3rem; margin-bottom: .9rem; font-weight: 550; font-size: .92rem; }
label .muted { font-weight: 400; }
label.check { flex-direction: row; align-items: center; gap: .6rem; font-weight: 400; }
.actions { display: flex; gap: .5rem; justify-content: flex-end; align-items: center; flex-wrap: wrap; margin-top: .75rem; }
.actions.left { justify-content: flex-start; margin: .5rem 0 0; }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; border-radius: 0; padding: .45rem .85rem; font-size: .88rem; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.active { background: var(--surface-2); }
.seg button.active.ok { background: var(--ok-bg); color: var(--ok); }
.seg button.active.bad { background: var(--bad-bg); color: var(--bad); }

.toolbar { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; margin-bottom: .75rem; }
.search { flex: 1; min-width: 160px; width: auto; }

/* ---- pills, dots, badges ---- */
.pill {
  display: inline-flex; align-items: center; gap: .25rem;
  padding: .12rem .55rem; border-radius: 999px;
  font-size: .78rem; font-weight: 600;
  background: var(--surface-2); color: var(--muted);
  white-space: nowrap;
}
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.bad { background: var(--bad-bg); color: var(--bad); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.new { background: var(--new-bg); color: var(--new); }
.pill.small { font-size: .72rem; }
.pill.mode-monitor { background: var(--new-bg); color: var(--new); }
.pill.mode-whitelist { background: var(--ok-bg); color: var(--ok); }
.pill.mode-blacklist { background: var(--bad-bg); color: var(--bad); }

.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; flex: none; background: var(--muted); }
.dot.on, .dot.ok { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-bg); }
.dot.off { background: var(--muted); }
.dot.bad { background: var(--bad); }

.badge {
  display: inline-grid; place-items: center;
  min-width: 1.35rem; height: 1.35rem; padding: 0 .35rem;
  border-radius: 999px; font-size: .74rem; font-weight: 700;
  background: var(--surface-2);
}
.badge.warn { background: var(--accent); color: var(--accent-text); }
.badge.bad { background: var(--bad); color: #fff; }

/* ---- login ---- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; padding: 28px 24px; }
.login-card h1 { text-align: center; margin-bottom: 1.25rem; }
.login-card button, .login-card .button { width: 100%; margin-top: .5rem; }
.login-card p { text-align: center; }
.login-logo { display: grid; place-items: center; margin-bottom: .5rem; }
.login-logo svg { width: 48px; height: 48px; color: var(--accent); }

/* ---- firewall list ---- */
.fwgrid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.fwcard { text-decoration: none; display: flex; flex-direction: column; gap: .5rem; margin: 0; transition: border-color .12s; }
.fwcard:hover { border-color: var(--accent); }
.fwcard-head { display: flex; align-items: center; gap: .5rem; font-size: 1.05rem; }
.fwcard-head strong { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fwcard-meta { display: flex; flex-direction: column; gap: .35rem; align-items: flex-start; }
.inline-form .actions { margin-top: 0; }

/* ---- modes ---- */
.modes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 1.25rem; }
.mode {
  flex-direction: column; align-items: flex-start; justify-content: flex-start; gap: .25rem;
  white-space: normal; text-align: left;
  padding: .8rem .9rem; border-radius: var(--radius);
  border-width: 2px;
}
.mode-title { font-weight: 650; }
.mode-help { font-weight: 400; font-size: .8rem; color: var(--muted); line-height: 1.35; }
.mode.active.mode-monitor { border-color: var(--new); background: var(--new-bg); }
.mode.active.mode-whitelist { border-color: var(--ok); background: var(--ok-bg); }
.mode.active.mode-blacklist { border-color: var(--bad); background: var(--bad-bg); }
@media (max-width: 640px) {
  .modes { grid-template-columns: 1fr; gap: 6px; }
  .mode { padding: .55rem .8rem; }
  .mode:not(.active) .mode-help { display: none; }
}

/* ---- tabs ---- */
.tabs {
  display: flex; gap: .25rem; margin-bottom: 1rem;
  border-bottom: 1px solid var(--border);
  overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  display: flex; align-items: center; gap: .35rem;
  padding: .6rem .8rem; text-decoration: none; color: var(--muted); font-weight: 550;
  border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap;
}
.tabs a.active { color: var(--text); border-bottom-color: var(--accent); }

/* ---- lists ---- */
.list { list-style: none; margin: 0; padding: 0; }
.req { display: flex; gap: .75rem; align-items: center; justify-content: space-between; flex-wrap: wrap; margin-bottom: 8px; }
.req-main { min-width: 0; flex: 1 1 260px; }
.req-domain { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; margin-bottom: .15rem; }
.req-domain strong { word-break: break-all; }
.req-actions { display: flex; gap: .35rem; flex-wrap: wrap; align-items: center; }
@media (max-width: 600px) {
  .req-actions { display: grid; grid-template-columns: repeat(4, 1fr); width: 100%; }
  .req-actions select { grid-column: 1 / -1; width: 100%; }
}

.act { padding: 0; margin-bottom: 8px; overflow: hidden; }
.act-row { display: flex; align-items: center; gap: .6rem; padding: 12px 14px; cursor: pointer; }
.act-row:hover { background: var(--surface-2); }
.act-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.act-main strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chev { color: var(--muted); font-size: 1.2rem; transition: transform .15s; width: .8rem; }
.chev.open { transform: rotate(90deg); }
.act-detail { padding: 0 14px 14px; border-top: 1px solid var(--border); overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: .85rem; margin-top: .5rem; }
th { text-align: left; color: var(--muted); font-weight: 550; padding: .4rem .5rem; border-bottom: 1px solid var(--border); }
td { padding: .4rem .5rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
td.mono { word-break: break-all; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---- rules ---- */
.rule-form { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.rule-form .grow { flex: 1 1 180px; width: auto; }
.rule-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 720px) { .rule-cols { grid-template-columns: 1fr; } }
.rules { list-style: none; margin: 0; padding: 0; }
.rules li { display: flex; align-items: center; justify-content: space-between; gap: .5rem; padding: .45rem 0; border-top: 1px solid var(--border); }
.rules li:first-child { border-top: 0; }
.rule-text { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; min-width: 0; }
.rule-text .mono { word-break: break-all; }
.rule-actions { display: flex; flex: none; }
.infra { margin-top: .75rem; border-top: 1px solid var(--border); padding-top: .6rem; }
.infra summary { cursor: pointer; color: var(--muted); font-size: .9rem; }
.infra p { margin: .5rem 0; }

/* ---- telegram & settings ---- */
.steps { padding-left: 1.2rem; margin: 0; }
.steps li { margin-bottom: 1rem; }
.settings .card { max-width: 640px; }
.danger { border-color: color-mix(in srgb, var(--bad) 35%, var(--border)); }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: 0 0 .5rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }

.tokenbox p { margin: .5rem 0; }
.copyrow { display: flex; gap: .5rem; align-items: center; background: var(--surface-2); border-radius: 9px; padding: .5rem .5rem .5rem .75rem; }
.copyrow code { flex: 1; overflow-x: auto; white-space: nowrap; }
.copyrow code.wrap { white-space: normal; word-break: break-all; }
