/* ==========================================================================
   Phonebook — stylesheet
   Palette: ink indigo sidebar, indigo actions, periwinkle tints, cool paper.
   Signature element: address-book index letters (login tabs + A–Z group rows).
   ========================================================================== */

:root {
  color-scheme: light;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;

  --ink: #1e1b4b;
  --ink-2: #2b2770;
  --primary: #4338ca;
  --primary-hover: #3730a3;
  --primary-active: #312e81;
  --primary-tint: #e0e7ff;
  --primary-tint-text: #3730a3;
  --accent-text: #4338ca;
  --ring: rgba(67, 56, 202, 0.4);

  --bg: #f5f6fa;
  --surface: #ffffff;
  --surface-2: #f8f9fc;
  --border: #e3e6ef;
  --border-strong: #c9cedb;
  --text: #1f2937;
  --text-2: #4b5563;
  --text-3: #6b7280;

  --danger: #b42318;
  --danger-hover: #912018;
  --danger-tint: #fef3f2;
  --danger-text: #912018;
  --success: #067647;
  --success-tint: #ecfdf3;
  --success-text: #05603a;
  --warning: #b54708;
  --warning-tint: #fffaeb;
  --warning-border: #fedf89;
  --warning-text: #93370d;
  --info-tint: #eff4ff;
  --info-text: #1d3c8f;

  --sidebar-text: #c7cbf5;
  --sidebar-strong: #ffffff;
  --sidebar-hover: rgba(255, 255, 255, 0.07);
  --sidebar-active: rgba(255, 255, 255, 0.13);
  --sidebar-rule: rgba(255, 255, 255, 0.12);

  --topbar-bg: rgba(245, 246, 250, 0.88);
  --backdrop: rgba(17, 16, 48, 0.5);

  --radius-sm: 6px;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow-sm: 0 1px 2px rgba(17, 24, 39, 0.06);
  --shadow-md: 0 2px 6px rgba(17, 24, 39, 0.06), 0 8px 24px rgba(30, 27, 75, 0.06);
  --shadow-lg: 0 24px 48px -16px rgba(30, 27, 75, 0.35);

  --sidebar-w: 240px;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --ink: #121032;
    --ink-2: #1b1848;
    --primary: #5b52e6;
    --primary-hover: #6d65f0;
    --primary-active: #4c43d4;
    --primary-tint: rgba(129, 120, 255, 0.18);
    --primary-tint-text: #c9c6ff;
    --accent-text: #aaa5ff;
    --ring: rgba(150, 142, 255, 0.55);

    --bg: #0e1020;
    --surface: #161a2e;
    --surface-2: #1b2036;
    --border: #282e48;
    --border-strong: #3a4263;
    --text: #e6e8f2;
    --text-2: #b3b9cf;
    --text-3: #8f97b2;

    --danger: #e5484d;
    --danger-hover: #f2555a;
    --danger-tint: rgba(229, 72, 77, 0.14);
    --danger-text: #ffa8a8;
    --success: #3dd68c;
    --success-tint: rgba(61, 214, 140, 0.12);
    --success-text: #8ef0bf;
    --warning: #f5a524;
    --warning-tint: rgba(245, 165, 36, 0.12);
    --warning-border: rgba(245, 165, 36, 0.35);
    --warning-text: #fcd38a;
    --info-tint: rgba(96, 140, 255, 0.14);
    --info-text: #b7c9ff;

    --topbar-bg: rgba(14, 16, 32, 0.86);
    --backdrop: rgba(4, 5, 14, 0.66);
    --shadow-md: 0 2px 6px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
    --shadow-lg: 0 24px 48px -16px rgba(0, 0, 0, 0.6);
  }
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2, h3, p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; }
code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.88em;
  padding: 0.1em 0.35em;
  border-radius: 4px;
  background: var(--primary-tint);
  color: var(--primary-tint-text);
}
kbd {
  display: inline-block;
  min-width: 1.6em;
  padding: 0 0.4em;
  font: inherit;
  font-size: 0.8em;
  line-height: 1.6;
  text-align: center;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  color: var(--text-2);
  background: var(--surface);
}
:focus-visible { outline: 3px solid var(--ring); outline-offset: 2px; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}
.icon {
  width: 18px; height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.noscript, .boot-error {
  margin: 24px; padding: 16px;
  border-radius: var(--radius);
  background: var(--danger-tint); color: var(--danger-text);
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px;
  padding: 0 16px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 14px;
  line-height: 1;
  white-space: nowrap;
  text-decoration: none;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
.btn .icon { width: 16px; height: 16px; }
.btn-sm { min-height: 34px; padding: 0 12px; font-size: 13px; }
.btn-block { width: 100%; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-primary:active:not(:disabled) { background: var(--primary-active); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn-secondary:hover:not(:disabled) { background: var(--surface-2); border-color: var(--text-3); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn:disabled { opacity: 0.5; }

.btn-icon {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px;
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-2);
}
.btn-icon:hover { background: var(--surface-2); color: var(--text); }
.btn-icon.is-danger:hover { background: var(--danger-tint); color: var(--danger); }

.spinner {
  display: none;
  width: 16px; height: 16px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.spinner-lg { display: inline-block; width: 28px; height: 28px; border-width: 3px; color: var(--primary); }
.btn.is-loading .spinner { display: inline-block; }
.btn.is-loading > .icon { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Forms ---------- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field label { font-size: 13px; font-weight: 600; color: var(--text); }
.req { color: var(--danger); }
.field input,
.field select,
.sort select {
  width: 100%;
  height: 42px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-variant-numeric: tabular-nums;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.field input:hover, .field select:hover, .sort select:hover { border-color: var(--text-3); }
.field input:focus, .field select:focus, .sort select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--ring);
}
.field input[aria-invalid="true"] { border-color: var(--danger); }
.field input[aria-invalid="true"]:focus { box-shadow: 0 0 0 3px rgba(180, 35, 24, 0.25); }
.input-wrap { position: relative; }
.input-wrap input { padding-left: 38px; }
.input-wrap.has-trailing input { padding-right: 44px; }
.input-icon { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--text-3); pointer-events: none; }
.input-trailing {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px;
  border: 0; border-radius: var(--radius-sm);
  background: transparent; color: var(--text-3);
}
.input-trailing:hover { color: var(--text); background: var(--surface-2); }
.field-error { font-size: 13px; color: var(--danger); }
.field-hint { font-size: 13px; color: var(--text-3); }
.field-hint-warn { color: var(--warning-text); }
.form-alert {
  display: flex; gap: 8px; align-items: flex-start;
  padding: 10px 12px;
  border-radius: var(--radius);
  background: var(--danger-tint);
  color: var(--danger-text);
  font-size: 14px;
}

/* ---------- Banners & pills ---------- */
.banner {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px;
  border: 1px solid;
  border-radius: var(--radius);
  font-size: 14px;
}
.banner-warning { background: var(--warning-tint); border-color: var(--warning-border); color: var(--warning-text); }
.banner-text { flex: 1; min-width: 0; }
.banner-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.banner-bar { margin: 16px 32px 0; }

.pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 13px; font-weight: 600;
  white-space: nowrap;
  background: var(--surface-2);
  color: var(--text-2);
  border: 1px solid var(--border);
}
.pill-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-3); }
.pill[data-state="saved"] { background: var(--success-tint); color: var(--success-text); border-color: transparent; }
.pill[data-state="saved"] .pill-dot { background: var(--success); }
.pill[data-state="dirty"] { background: var(--warning-tint); color: var(--warning-text); border-color: var(--warning-border); }
.pill[data-state="dirty"] .pill-dot { background: var(--warning); }
.pill[data-state="saving"] { background: var(--primary-tint); color: var(--primary-tint-text); border-color: transparent; }
.pill[data-state="saving"] .pill-dot { background: var(--primary); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.3; } }

/* ==========================================================================
   Login
   ========================================================================== */
.login-view {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 32px 16px;
  background:
    linear-gradient(180deg, var(--ink) 0, var(--ink) 38vh, var(--bg) 38vh);
}
.login-frame { width: 100%; max-width: 460px; }
.login-brand {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 20px;
  color: #fff;
  font-size: 18px; font-weight: 700;
  letter-spacing: -0.01em;
}
.icon-brand { width: 26px; height: 26px; color: #a5b4fc; }
.login-card {
  display: flex;
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.login-body { flex: 1; min-width: 0; padding: 32px 32px 28px; }
.login-title { font-size: 26px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.2; }
.login-lede { margin-top: 6px; color: var(--text-2); }
#login-notice { margin-top: 16px; }

/* Index tabs down the card's edge, like a paper address book */
.index-tabs {
  display: flex; flex-direction: column; justify-content: space-between;
  width: 26px;
  padding: 14px 0;
  background: var(--primary-tint);
  border-left: 1px solid var(--border);
  color: var(--primary-tint-text);
  font-size: 10px; font-weight: 700;
  line-height: 1;
  text-align: center;
  user-select: none;
}

.login-form { display: flex; flex-direction: column; gap: 16px; margin-top: 20px; }
.login-form .btn-primary { min-height: 44px; margin-top: 4px; font-size: 15px; }

@media (max-width: 480px) {
  .login-view { padding: 20px 12px; place-items: start center; }
  .login-body { padding: 24px 20px; }
  .index-tabs { display: none; }
}

/* ==========================================================================
   App shell
   ========================================================================== */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  min-height: 100vh;
}
.sidebar {
  position: sticky; top: 0;
  height: 100vh; height: 100dvh;
  display: flex; flex-direction: column;
  padding: 18px 12px;
  background: var(--ink);
  color: var(--sidebar-text);
  overflow-y: auto;
}
.sidebar-brand {
  display: flex; align-items: center; gap: 10px;
  padding: 4px 10px 20px;
  color: var(--sidebar-strong);
  font-size: 17px; font-weight: 700;
}
.sidebar-close { display: none; margin-left: auto; color: var(--sidebar-text); }
.sidebar-close:hover { background: var(--sidebar-hover); color: #fff; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 12px;
  width: 100%;
  padding: 9px 10px;
  border: 0; border-radius: var(--radius);
  background: transparent;
  color: var(--sidebar-text);
  font-size: 14px; font-weight: 500;
  text-align: left;
}
.nav-item:hover { background: var(--sidebar-hover); color: var(--sidebar-strong); }
.nav-item[aria-current="page"] { background: var(--sidebar-active); color: var(--sidebar-strong); }
.nav-item:focus-visible { outline-color: #a5b4fc; }
.nav-count {
  margin-left: auto;
  padding: 1px 8px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.sidebar-footer { margin-top: auto; padding-top: 14px; border-top: 1px solid var(--sidebar-rule); }
.user-chip { display: flex; align-items: center; gap: 10px; padding: 6px 10px 10px; }
.user-initial {
  display: grid; place-items: center;
  width: 32px; height: 32px;
  border-radius: 50%;
  background: #a5b4fc; color: var(--ink);
  font-weight: 700; font-size: 14px;
}
.user-meta { display: flex; flex-direction: column; min-width: 0; line-height: 1.3; }
.user-name { color: var(--sidebar-strong); font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-role { font-size: 12px; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  min-height: 60px;
  padding: 10px 32px;
  background: var(--topbar-bg);
  border-bottom: 1px solid var(--border);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}
.menu-button { display: none; margin-left: -8px; }
.topbar-brand {
  display: none; align-items: center; gap: 8px;
  flex: 1; min-width: 0;
  font-size: 17px; font-weight: 700; letter-spacing: -0.01em;
  color: var(--text);
}
.topbar-brand .icon-brand { width: 22px; height: 22px; color: var(--accent-text); }
.topbar-file {
  flex: 1; min-width: 0;
  font-size: 14px; color: var(--text-2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.view { padding: 28px 32px 56px; max-width: 1200px; width: 100%; }
.page-head {
  display: flex; align-items: flex-end; justify-content: space-between; gap: 16px;
  margin-bottom: 20px;
}
.page-title { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.15; }
.page-title:focus { outline: none; }
.page-count { margin-top: 4px; color: var(--text-2); font-variant-numeric: tabular-nums; }
.page-count strong { color: var(--text); font-weight: 600; }

/* ---------- Toolbar ---------- */
/* Stays under the top bar while the list scrolls, so search is always at hand. */
.toolbar {
  position: sticky; top: 60px; z-index: 15;
  display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
  margin: -8px -32px 8px;
  padding: 8px 32px;
  background: var(--bg);
}
.search { position: relative; flex: 1 1 320px; max-width: 520px; }
.search input {
  width: 100%; height: 42px;
  padding: 0 40px 0 40px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}
.search input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--ring); }
.search input:disabled, .sort select:disabled { opacity: 0.55; cursor: not-allowed; }
.search-icon { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: var(--text-3); pointer-events: none; }
.search-kbd { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); pointer-events: none; }
.search input:focus ~ .search-kbd, .search input:not(:placeholder-shown) ~ .search-kbd { display: none; }
.sort { display: flex; align-items: center; gap: 8px; }
.sort label { font-size: 14px; color: var(--text-2); font-weight: 500; }
.sort select { width: auto; min-width: 160px; padding-right: 32px; }

/* ---------- Panels ---------- */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.panel-pad { padding: 20px 22px; }
.panel-title { font-size: 16px; font-weight: 700; margin-bottom: 14px; }
.panel-text { margin-top: 14px; font-size: 14px; color: var(--text-2); }

/* ---------- States ---------- */
.state {
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 56px 24px;
  text-align: center;
}
.state-icon {
  width: 44px; height: 44px;
  padding: 10px;
  margin-bottom: 4px;
  border-radius: 50%;
  background: var(--primary-tint);
  color: var(--primary);
  stroke-width: 1.8;
}
.state-error .state-icon { background: var(--danger-tint); color: var(--danger); }
.state-title { font-size: 18px; font-weight: 700; }
.state-text { max-width: 52ch; color: var(--text-2); overflow-wrap: anywhere; }
.state .btn { margin-top: 8px; }

/* ---------- Contacts table ---------- */
.table-wrap { overflow-x: auto; border-radius: var(--radius-lg); }
.contacts-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.contacts-table th {
  padding: 10px 16px;
  text-align: left;
  font-size: 13px; font-weight: 600;
  color: var(--text-3);
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.th-sort {
  display: inline-flex; align-items: center; gap: 6px;
  margin: -4px -6px; padding: 4px 6px;
  border: 0; border-radius: var(--radius-sm);
  background: transparent;
  font: inherit; color: var(--text-2);
}
.th-sort:hover { color: var(--text); background: var(--border); }
.sort-arrow { width: 14px; height: 14px; transition: transform 0.15s ease; }
.sort-arrow.is-desc { transform: rotate(180deg); }
.contacts-table td {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.contacts-table tbody tr:last-child td { border-bottom: 0; }
.contact-row:hover { background: var(--surface-2); }
.contact-row.is-flash { animation: flash 1.6s ease-out; }
@keyframes flash { 0%, 30% { background: var(--primary-tint); } 100% { background: transparent; } }

/* A–Z index group rows */
.group-row td {
  padding: 14px 16px 4px;
  border-bottom: 0;
  background: transparent;
}
.group-letter {
  display: inline-block;
  min-width: 28px;
  font-size: 20px; font-weight: 800;
  line-height: 1;
  color: var(--accent-text);
  letter-spacing: -0.02em;
}
.group-row + .contact-row td { border-top: 1px solid var(--border); }

.name-cell { display: flex; align-items: center; gap: 12px; min-width: 220px; }
.avatar {
  display: grid; place-items: center;
  width: 34px; height: 34px;
  flex: none;
  border-radius: 50%;
  font-size: 13px; font-weight: 700;
  background: var(--primary-tint);
  color: var(--primary-tint-text);
}
.name-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.3; }
.name-primary { font-weight: 600; color: var(--text); overflow-wrap: anywhere; }
.name-primary.is-empty { font-weight: 500; color: var(--text-3); font-style: italic; }
.name-id { font-size: 12px; color: var(--text-3); overflow-wrap: anywhere; }
.cell-number { font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--text); }
.cell-number .empty { color: var(--border-strong); }
.tel-link { color: inherit; text-decoration: none; border-radius: 4px; }
.tel-link:hover { color: var(--accent-text); text-decoration: underline; text-underline-offset: 3px; }
.cell-actions { text-align: right; white-space: nowrap; width: 1%; }
.cell-actions .btn-icon { width: 34px; height: 34px; }
mark { background: #fde68a; color: inherit; border-radius: 2px; padding: 0 1px; }

/* ---------- Data files view ---------- */
.files-grid { display: grid; grid-template-columns: minmax(0, 640px); gap: 20px; }
.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 8px 20px; margin: 0; font-size: 14px; }
.kv dt { color: var(--text-3); }
.kv dd { margin: 0; color: var(--text); overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
.button-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 20px; }
.status-ok { color: var(--success-text); font-weight: 600; }
.status-warn { color: var(--warning-text); font-weight: 600; }

/* ---------- Desk phones view ---------- */
.phones-grid { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); gap: 20px; align-items: start; }
.remote-status { display: flex; align-items: center; gap: 10px; color: var(--text-2); font-size: 14px; }
.spinner-sm { display: inline-block; color: var(--primary); }
.remote-form { display: flex; flex-direction: column; gap: 16px; }
.remote-form .button-row { margin-top: 0; }
.copy-row { display: flex; gap: 8px; }
.copy-row input {
  flex: 1; min-width: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  background: var(--surface-2);
}
.copy-row .btn { min-height: 42px; }
.steps { margin: 0 0 12px; padding-left: 22px; display: grid; gap: 8px; color: var(--text-2); }
.steps strong { color: var(--text); }
a.btn { color: inherit; }
a.btn-primary { color: #fff; }

/* ---------- Help view ---------- */
.prose { max-width: 72ch; }
.prose h2 { font-size: 17px; font-weight: 700; margin: 24px 0 8px; }
.prose h2:first-child { margin-top: 0; }
.prose p, .prose li { color: var(--text-2); }
.prose p + p { margin-top: 10px; }
.prose ul { margin: 0; padding-left: 20px; display: grid; gap: 6px; }

/* ==========================================================================
   Dialogs
   ========================================================================== */
.dialog {
  width: min(540px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 0;
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
}
.dialog-sm { width: min(440px, calc(100vw - 32px)); }
.dialog::backdrop { background: var(--backdrop); }
.dialog[open] { animation: dialog-in 0.16s ease-out; }
@keyframes dialog-in { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
.dialog-header {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  padding: 20px 24px 4px;
}
.dialog-title { font-size: 19px; font-weight: 700; letter-spacing: -0.01em; }
.dialog-sub { margin-top: 2px; font-size: 13px; color: var(--text-3); overflow-wrap: anywhere; }
.dialog-header .btn-icon { margin: -6px -10px 0 0; }
.dialog-body { padding: 14px 24px 20px; }
.dialog-body > .form-alert { margin-bottom: 14px; }
.dialog-footer {
  display: flex; justify-content: flex-end; gap: 10px;
  padding: 14px 24px;
  background: var(--surface-2);
  border-top: 1px solid var(--border);
}
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 16px; }
.span-2 { grid-column: 1 / -1; }
.form-note { margin-top: 14px; font-size: 13px; color: var(--text-3); }
.confirm-message { color: var(--text-2); overflow-wrap: anywhere; }

/* ==========================================================================
   Toasts
   ========================================================================== */
.toast-region {
  position: fixed; right: 20px; bottom: 20px; z-index: 1000;
  display: flex; flex-direction: column; gap: 10px;
  width: min(380px, calc(100vw - 32px));
  pointer-events: none;
}
.toast {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 12px 12px 12px 14px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--text-3);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-md);
  pointer-events: auto;
  animation: toast-in 0.2s ease-out;
}
.toast.is-leaving { opacity: 0; transform: translateY(6px); transition: opacity 0.18s ease, transform 0.18s ease; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
.toast-success { border-left-color: var(--success); }
.toast-success .toast-icon { color: var(--success); }
.toast-error { border-left-color: var(--danger); }
.toast-error .toast-icon { color: var(--danger); }
.toast-warning { border-left-color: var(--warning); }
.toast-warning .toast-icon { color: var(--warning); }
.toast-info { border-left-color: var(--primary); }
.toast-info .toast-icon { color: var(--primary); }
.toast-icon { display: flex; padding-top: 1px; }
.toast-body { flex: 1; min-width: 0; }
.toast-title { font-weight: 600; font-size: 14px; }
.toast-message { margin-top: 2px; font-size: 13px; color: var(--text-2); overflow-wrap: anywhere; }
.toast .btn-icon { width: 28px; height: 28px; margin: -4px -4px 0 0; }
.toast .btn-icon .icon { width: 16px; height: 16px; }

/* ==========================================================================
   Responsive
   ========================================================================== */
@media (max-width: 1024px) {
  .app-shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 60;
    width: min(280px, 85vw);
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform 0.22s ease, visibility 0s linear 0.22s;
  }
  .app-shell.sidebar-open .sidebar {
    transform: none;
    visibility: visible;
    box-shadow: var(--shadow-lg);
    transition: transform 0.22s ease, visibility 0s;
  }
  .sidebar-backdrop { position: fixed; inset: 0; z-index: 50; background: var(--backdrop); }
  .sidebar-close, .menu-button { display: inline-flex; }
  .files-grid, .phones-grid { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 760px) {
  .topbar { padding: 8px 16px; }
  .topbar-brand { display: flex; }
  .topbar-file { display: none; }
  .view { padding: 20px 16px 48px; }
  #view-contacts { padding-bottom: 104px; } /* room for the floating Add button */
  .banner-bar { margin: 12px 16px 0; flex-wrap: wrap; }
  .page-title { font-size: 24px; }
  .page-head { align-items: center; margin-bottom: 14px; }

  /* Search and sort share one sticky row */
  .toolbar { gap: 8px; flex-wrap: nowrap; margin: -8px -16px 8px; padding: 8px 16px; }
  .search { flex: 1 1 auto; min-width: 0; max-width: none; }
  .search input { padding-right: 10px; }
  .search-kbd { display: none; }
  .sort label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .sort select { min-width: 0; width: auto; padding-left: 10px; }

  /* 16px text stops phones from zooming into a field when it is tapped */
  .field input, .field select, .search input, .sort select { font-size: 16px; }
  .copy-row input { font-size: 13px; } /* read-only, so no zoom on tap */

  /* Add contact floats at the bottom corner, within thumb reach */
  #btn-add {
    position: fixed; z-index: 40;
    right: 16px; bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    min-height: 54px; padding: 0 22px;
    border-radius: 999px;
    font-size: 15px;
    box-shadow: 0 6px 20px rgba(30, 27, 75, 0.35);
  }
  #btn-add .icon { width: 20px; height: 20px; }
  #btn-add:disabled { opacity: 0; pointer-events: none; }
  .toast-region { bottom: calc(86px + env(safe-area-inset-bottom, 0px)); }

  /* Table becomes a list of compact cards */
  .contacts-table thead {
    position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0);
  }
  .contacts-table, .contacts-table tbody, .contacts-table tr, .contacts-table td { display: block; }
  .contacts-table tbody { padding: 2px 10px 10px; }
  .group-row td { padding: 12px 4px 6px; }
  .group-letter { font-size: 18px; }
  .group-row + .contact-row td { border-top: 0; }
  .contacts-table tr.contact-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    column-gap: 4px;
    margin-top: 8px;
    padding: 10px 6px 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
  }
  .group-row + .contact-row { margin-top: 0; }
  .contact-row:hover { background: var(--surface); }
  .contacts-table td { padding: 0; border: 0; }
  .contacts-table td.cell-name { grid-column: 1; grid-row: 1; padding-bottom: 4px; }
  .contacts-table td.cell-actions {
    grid-column: 2; grid-row: 1;
    display: flex; align-items: flex-start;
    width: auto;
  }
  .contacts-table td.cell-number {
    grid-column: 1 / -1;
    display: flex; justify-content: space-between; align-items: center; gap: 12px;
    padding: 2px 8px 2px 46px;
    min-height: 32px;
  }
  .contacts-table td.cell-number.is-empty { display: none; }
  .contacts-table td.cell-number::before {
    content: attr(data-label);
    color: var(--text-3);
    font-size: 13px;
  }
  .tel-link { color: var(--accent-text); font-weight: 600; padding: 4px 0; }
  .name-cell { min-width: 0; }
  .name-id { display: none; }
}

@media (max-width: 480px) {
  .form-grid { grid-template-columns: minmax(0, 1fr); }
  .kv { grid-template-columns: minmax(0, 1fr); gap: 2px; }
  .kv dd { margin-bottom: 8px; }
  .button-row .btn { flex: 1 1 100%; min-height: 44px; }
  .dialog-header { padding: 18px 18px 4px; }
  .dialog-body { padding: 12px 18px 16px; }
  .dialog-footer { padding: 12px 18px; }
  .dialog-footer .btn { flex: 1; min-height: 46px; }
  .toast-region { left: 16px; right: 16px; width: auto; }
}

/* Touch screens: bigger targets for icon buttons */
@media (pointer: coarse) {
  .cell-actions .btn-icon, .btn-icon { width: 42px; height: 42px; }
  .toast .btn-icon { width: 36px; height: 36px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
