:root {
      --bg: #080c14;
      --panel: #101726;
      --panel-2: #162033;
      --panel-3: #1d2a42;
      --text: #f3f6fb;
      --muted: #94a3b8;
      --line: #26344d;
      --accent: #10b981;
      --accent-soft: rgba(16, 185, 129, 0.14);
      --warn: #f59e0b;
      --warn-soft: rgba(245, 158, 11, 0.14);
      --danger: #ef4444;
      --danger-soft: rgba(239, 68, 68, 0.14);
      --info: #3b82f6;
      --info-soft: rgba(59, 130, 246, 0.12);
    }
    *, *::before, *::after {
      box-sizing: border-box;
    }
    html, body {
      margin: 0;
      padding: 0;
      width: 100%;
      max-width: 100%;
      overflow-x: hidden;
      font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
      background: var(--bg);
      color: var(--text);
      line-height: 1.48;
      -webkit-font-smoothing: antialiased;
    }
    header {
      padding: 16px 20px;
      border-bottom: 1px solid var(--line);
      background: rgba(14, 20, 32, 0.94);
      backdrop-filter: blur(10px);
      position: sticky;
      top: 0;
      z-index: 30;
      display: flex;
      flex-wrap: wrap;
      gap: 12px;
      align-items: center;
      justify-content: space-between;
    }
    h1 { font-size: 18px; font-weight: 700; margin: 0; letter-spacing: -0.01em; }
    h2 { font-size: 16px; font-weight: 700; margin: 0 0 6px; letter-spacing: -0.01em; }
    h3 { font-size: 13.5px; font-weight: 600; margin: 12px 0 8px; color: #e2e8f0; }
    p { margin: 4px 0; }
    code {
      font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
      font-size: 12px;
      background: rgba(15, 23, 42, 0.75);
      border: 1px solid var(--line);
      padding: 2px 6px;
      border-radius: 5px;
      color: #6ee7b7;
      word-break: break-word;
    }
    .muted { color: var(--muted); font-size: 13px; }
    main {
      width: 100%;
      max-width: 1180px;
      margin: 0 auto;
      padding: 16px;
    }
    .bar {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
      align-items: center;
      min-width: 0;
      max-width: 100%;
    }
    input:not([type="checkbox"]), textarea, button, select {
      font: inherit;
      font-size: 13.5px;
      color: var(--text);
      background: var(--panel-2);
      border: 1px solid var(--line);
      border-radius: 9px;
      padding: 9px 12px;
      max-width: 100%;
      min-width: 0;
      transition: border-color 0.15s ease, background-color 0.15s ease, box-shadow 0.15s ease;
    }
    input:not([type="checkbox"]):focus, textarea:focus, select:focus {
      outline: none;
      border-color: var(--accent);
      box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.15);
    }
    /* Custom styled select dropdown that never overflows mobile screens */
    .select-wrap {
      position: relative;
      display: inline-flex;
      align-items: center;
      width: 100%;
      max-width: 100%;
      min-width: 0;
    }
    .select-wrap select, select.custom-select {
      appearance: none;
      -webkit-appearance: none;
      -moz-appearance: none;
      width: 100%;
      max-width: 100%;
      min-width: 0;
      padding-right: 36px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      background-color: var(--panel-2);
      background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
      background-repeat: no-repeat;
      background-position: right 11px center;
      background-size: 15px;
      cursor: pointer;
    }
    select {
      appearance: none;
      -webkit-appearance: none;
      -moz-appearance: none;
      max-width: 100%;
      min-width: 0;
      padding-right: 34px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
      background-repeat: no-repeat;
      background-position: right 10px center;
      background-size: 14px;
      cursor: pointer;
    }
    button {
      cursor: pointer;
      background: #1e293b;
      font-weight: 600;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      gap: 6px;
      white-space: nowrap;
    }
    button:hover:not(:disabled) {
      border-color: var(--accent);
      background: #243247;
    }
    button.primary {
      background: var(--accent);
      color: #03150e;
      border-color: var(--accent);
      font-weight: 700;
    }
    button.primary:hover:not(:disabled) {
      background: #34d399;
      border-color: #34d399;
    }
    button.warn {
      border-color: rgba(245, 158, 11, 0.5);
      color: #fde68a;
      background: rgba(245, 158, 11, 0.12);
    }
    button.danger {
      border-color: rgba(239, 68, 68, 0.45);
      color: #fca5a5;
      background: rgba(239, 68, 68, 0.12);
    }
    button:disabled { opacity: 0.45; cursor: not-allowed; }
    .tabs {
      display: flex;
      gap: 8px;
      margin-bottom: 16px;
      overflow-x: auto;
      padding-bottom: 4px;
      scrollbar-width: thin;
    }
    .tabs button {
      flex: 0 0 auto;
      padding: 9px 14px;
      font-size: 13px;
      border-radius: 9px;
    }
    .tabs button.active {
      background: var(--accent);
      color: #04130d;
      border-color: var(--accent);
      font-weight: 700;
    }
    .grid {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 16px;
      width: 100%;
    }
    .grid > * {
      min-width: 0;
      max-width: 100%;
    }
    .grid-3 {
      display: grid;
      grid-template-columns: repeat(3, minmax(0, 1fr));
      gap: 10px;
      width: 100%;
    }
    .grid-3 > * {
      min-width: 0;
      max-width: 100%;
    }
    .card {
      background: var(--panel);
      border: 1px solid var(--line);
      border-radius: 12px;
      padding: 16px;
      margin-bottom: 16px;
      width: 100%;
      max-width: 100%;
      min-width: 0;
      overflow: hidden;
    }
    .section-save-bar {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      justify-content: space-between;
      gap: 10px;
      padding: 12px 14px;
      margin-bottom: 14px;
      border-radius: 10px;
      background: rgba(15, 23, 42, 0.85);
      border: 1px solid var(--line);
    }
    .section-save-bar.dirty {
      border-color: var(--warn);
      background: rgba(245, 158, 11, 0.09);
    }
    .badge {
      display: inline-flex;
      align-items: center;
      gap: 5px;
      padding: 4px 10px;
      border-radius: 7px;
      font-size: 12px;
      font-weight: 700;
      border: 1px solid var(--line);
      background: var(--panel-2);
      max-width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .badge.ok { color: #6ee7b7; border-color: rgba(16, 185, 129, 0.4); background: var(--accent-soft); }
    .badge.warn { color: #fde68a; border-color: rgba(245, 158, 11, 0.45); background: var(--warn-soft); }
    .mode-card {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      text-align: left;
      padding: 14px 15px;
      min-height: 88px;
      border-radius: 10px;
      border: 1px solid var(--line);
      background: var(--panel-2);
      cursor: pointer;
      width: 100%;
      min-width: 0;
      white-space: normal;
      touch-action: manipulation;
      user-select: none;
      -webkit-tap-highlight-color: transparent;
      transition: border-color 0.15s, background 0.15s;
    }
    .mode-card input[type="radio"] {
      width: 20px;
      height: 20px;
      margin: 0 0 9px;
      accent-color: var(--accent);
      flex: 0 0 auto;
      cursor: pointer;
      pointer-events: auto;
    }
    .mode-card:hover {
      border-color: #3b4f73;
    }
    .mode-card.active {
      border-color: var(--accent);
      background: var(--accent-soft);
      box-shadow: inset 0 0 0 1px var(--accent);
    }
    .mode-card .mode-title {
      font-size: 13.5px;
      font-weight: 700;
      color: var(--text);
      margin-bottom: 3px;
    }
    .mode-card.active .mode-title {
      color: #6ee7b7;
    }
    .mode-card .mode-desc {
      font-size: 12px;
      color: var(--muted);
      font-weight: 400;
      line-height: 1.35;
    }
    .row {
      display: flex;
      justify-content: space-between;
      align-items: center;
      gap: 12px;
      padding: 10px 0;
      border-bottom: 1px solid rgba(39, 50, 74, 0.65);
      min-width: 0;
    }
    .row > div:first-child {
      min-width: 0;
      flex: 1 1 auto;
    }
    .row:last-child { border-bottom: 0; }
    .list {
      max-height: 320px;
      overflow-y: auto;
      overflow-x: hidden;
      border: 1px solid var(--line);
      border-radius: 10px;
      padding: 6px 8px;
      background: #0b111d;
      width: 100%;
    }
    label.check {
      display: flex;
      gap: 10px;
      align-items: center;
      padding: 9px 6px;
      border-bottom: 1px solid rgba(31, 41, 55, 0.55);
      cursor: pointer;
      font-size: 13px;
      border-radius: 6px;
      min-width: 0;
      user-select: none;
    }
    label.check:hover {
      background: rgba(255, 255, 255, 0.03);
    }
    label.check.is-checked {
      background: rgba(16, 185, 129, 0.08);
    }
    label.check span {
      min-width: 0;
      overflow: hidden;
      text-overflow: ellipsis;
      word-break: break-word;
    }
    label.check:last-child { border-bottom: 0; }
    input[type="checkbox"] {
      width: 18px;
      height: 18px;
      flex: 0 0 18px;
      accent-color: var(--accent);
      cursor: pointer;
      margin: 0;
    }
    .toast {
      position: fixed;
      right: 16px;
      bottom: 16px;
      left: 16px;
      margin-left: auto;
      background: #0f172a;
      border: 1px solid var(--accent);
      padding: 12px 16px;
      border-radius: 10px;
      display: none;
      max-width: 420px;
      box-shadow: 0 12px 28px rgba(0,0,0,0.55);
      z-index: 60;
      font-size: 13px;
      font-weight: 500;
    }
    .hidden { display: none !important; }
    .pill-group { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
    .scope-btn {
      padding: 7px 11px;
      font-size: 12.5px;
      border-radius: 7px;
      background: var(--panel-2);
      border: 1px solid var(--line);
      white-space: normal;
      text-align: left;
    }
    .scope-btn.active {
      border-color: var(--accent);
      background: var(--accent-soft);
      color: #6ee7b7;
      font-weight: 700;
    }
    .explainer {
      padding: 12px 14px;
      border-radius: 10px;
      background: var(--info-soft);
      border: 1px solid rgba(59, 130, 246, 0.3);
      font-size: 13px;
      margin-bottom: 14px;
    }
    .input-row {
      display: flex;
      gap: 8px;
      width: 100%;
      margin-bottom: 8px;
    }
    .input-row input {
      flex: 1 1 auto;
      min-width: 0;
    }
    @media (max-width: 820px) {
      main { padding: 12px; }
      header { padding: 12px 14px; }
      .grid, .grid-3 {
        grid-template-columns: 1fr;
      }
      .card { padding: 13px; }
      .section-save-bar {
        flex-direction: column;
        align-items: stretch;
      }
      .section-save-bar .bar {
        width: 100%;
        justify-content: stretch;
      }
      .section-save-bar .bar button {
        flex: 1 1 auto;
      }
    }
    @media (max-width: 480px) {
      h1 { font-size: 16px; }
      h2 { font-size: 15px; }
      .input-row {
        flex-direction: column;
      }
      .input-row button {
        width: 100%;
      }
    }


/* Selected destination chips keep chosen groups/contacts visible while scrolling. */
.selected-summary { margin: 10px 0 14px; padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--panel-2); }
.selected-chips { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 8px; max-height: 150px; overflow: auto; }
.selected-chip { display: inline-flex; align-items: center; gap: 7px; max-width: 100%; border: 1px solid var(--line); border-radius: 999px; padding: 6px 10px; color: var(--text); background: var(--panel-3); font: inherit; font-size: 12px; cursor: pointer; }
.selected-chip span { overflow-wrap: anywhere; }
.selected-chip .remove { color: var(--warn); font-weight: 800; }
#passcodeLoginForm, #tokenLoginForm { min-width: 0; }


/* Blocking progress state for login, state loading, and settings writes. */
.portal-loading-overlay { position: fixed; inset: 0; z-index: 10000; display: none; align-items: center; justify-content: center; padding: 20px; background: rgba(3, 7, 18, .74); backdrop-filter: blur(5px); }
.portal-loading-overlay.visible { display: flex; }
.portal-loading-card { display: flex; flex-direction: column; align-items: center; gap: 10px; width: min(320px, 100%); padding: 24px; border: 1px solid var(--line); border-radius: 16px; background: var(--panel); box-shadow: 0 18px 60px rgba(0,0,0,.38); text-align: center; }
.portal-spinner { width: 34px; height: 34px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: portal-spin .8s linear infinite; }
@keyframes portal-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .portal-spinner { animation-duration: 2s; } }
