/* ==========================================================================
   Surface: Settings (#page-settings) + aliases + security dialogs
   (owned by dashboard-core; markup from account-services.js and dash-aliases.js)
   Owns: .settings-page, .settings-layout, .settings-nav[role=tablist] > [role=tab]
   (.is-danger), .settings-panels > .settings-panel, .settings-card, .settings-identity,
   .identity-chips, .settings-row (-icon/-text/-value/-action, .is-danger), .settings-note,
   .session-list > .session-row(.is-current) (.session-icon/-text), .conn-tile,
   .danger-zone, .aliases-card (.alias-meter, .alias-list > .alias-row(.is-main/.is-inactive),
   .alias-icon/-text/-actions, .alias-form, .alias-status[data-kind], .alias-upsell),
   dialogs: .mfa-setup (.mfa-qr, .mfa-steps), .recovery-codes, .delete-list, .auth-error.
   r181 (unslop): quiet rows (one 24px icon column in the muted role, no tiles), no washes, the
   selected tab is a surface step with a hairline; addresses use the signature (.nv-address).
   ========================================================================== */

@layer surfaces {
  .settings-layout {
    display: grid;
    grid-template-columns: 220px minmax(0, 1fr);
    gap: var(--nv-space-6);
    align-items: start;
  }

  /* Section tabs: navigation rows like the sidebar (36px, 2px apart, one 18px icon column). */
  .settings-nav {
    position: sticky;
    top: calc(var(--nv-topbar-h) + var(--nv-space-5));
    display: grid;
    gap: 2px;
    padding: var(--nv-space-2);
    border: 1px solid var(--nv-line);
    border-radius: var(--nv-r-lg);
    background: var(--nv-surface-1);
  }

  .settings-nav [role="tab"] {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: var(--nv-h-nav);
    padding: 0 var(--nv-space-3);
    border-radius: var(--nv-r-sm);
    color: var(--nv-text-2);
    font-size: var(--nv-fs-14);
    font-weight: var(--nv-fw-medium);
    text-align: left;
    white-space: nowrap;
    transition: background-color var(--nv-dur-1) var(--nv-ease), color var(--nv-dur-1) var(--nv-ease);
  }

  .settings-nav [role="tab"]::after {
    display: none;
  }

  .settings-nav [role="tab"] .nv-icon {
    flex: none;
    width: 18px;
    height: 18px;
    color: var(--nv-text-3);
  }

  .settings-nav [role="tab"]:hover {
    background: var(--nv-surface-2);
    color: var(--nv-text);
  }

  /* The open section: one surface step with a hairline; text and icon in the primary role. */
  .settings-nav [role="tab"][aria-selected="true"] {
    background: var(--nv-surface-3);
    color: var(--nv-text);
    box-shadow: inset 0 0 0 1px var(--nv-line);
  }

  .settings-nav [role="tab"][aria-selected="true"] .nv-icon {
    color: var(--nv-text);
  }

  .settings-panel {
    display: grid;
    gap: var(--nv-space-4);
    outline: none;
    animation: shell-page-in var(--nv-dur-3) var(--nv-ease);
  }

  .settings-panel[hidden] {
    display: none;
  }

  .settings-card {
    display: grid;
    gap: 0;
    padding: var(--nv-space-5) var(--nv-space-6);
  }

  .settings-card > .card-head {
    margin-bottom: var(--nv-space-2);
  }

  /* The head's muted subtitle style (components: .card-head span) is not for a button's label
     («Завершить другие» became a 13px grey block that wrapped). */
  .settings-card > .card-head > :is(button, a) > span {
    display: inline;
    margin-top: 0;
    color: inherit;
    font-size: inherit;
    white-space: nowrap;
  }

  .settings-card .card-head h2 {
    font-size: var(--nv-fs-18);
  }

  .settings-identity {
    display: flex;
    align-items: center;
    gap: var(--nv-space-5);
  }

  .settings-identity .avatar.xl {
    --av: 72px;
  }

  .settings-identity h2 {
    font-size: var(--nv-fs-22);
    letter-spacing: 0;
  }

  .settings-identity p {
    margin-top: var(--nv-space-1);
    color: var(--nv-text-2);
  }

  .identity-chips {
    display: flex;
    flex-wrap: wrap;
    gap: var(--nv-space-2);
    margin-top: var(--nv-space-3);
  }

  .settings-row {
    display: flex;
    align-items: center;
    gap: var(--nv-space-3);
    min-height: 64px;
    padding: var(--nv-space-3) 0;
  }

  .settings-row + .settings-row,
  .card-head + .settings-row ~ .settings-row {
    border-top: 1px solid var(--nv-line-soft);
  }

  /* One 24px icon column: line icons in the muted role (no tile); provider marks fill it. */
  .settings-row-icon {
    display: grid;
    flex: none;
    place-items: center;
    width: 24px;
    height: 24px;
    color: var(--nv-text-3);
  }

  .settings-row-icon > .nv-icon {
    width: 18px;
    height: 18px;
  }

  .settings-row-icon > .conn-tile {
    --tile: 24px;
    border: 0;
  }

  .conn-tile svg {
    width: 56%;
    height: 56%;
  }

  .settings-row-text {
    display: grid;
    flex: 1;
    gap: 2px;
    min-width: 0;
  }

  .settings-row-text b {
    color: var(--nv-text);
    font-size: var(--nv-fs-14);
    font-weight: var(--nv-fw-medium);
  }

  .settings-row-text span {
    overflow-wrap: anywhere;
    color: var(--nv-text-3);
    font-size: var(--nv-fs-13);
    line-height: var(--nv-lh-ui);
  }

  .settings-row-value,
  .settings-row-action {
    flex: none;
  }

  /* A status chip and two buttons in one row keep the 8px control gap and wrap as a group. */
  .settings-row-action {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: var(--nv-space-2);
  }

  .settings-row-action .ghost .nv-icon {
    width: 16px;
    height: 16px;
  }

  .settings-note {
    display: flex;
    align-items: flex-start;
    gap: var(--nv-space-2);
    margin-top: var(--nv-space-3);
    padding: var(--nv-space-3);
    border-radius: var(--nv-r-md);
    background: var(--nv-surface-2);
    color: var(--nv-text-2);
    font-size: var(--nv-fs-13);
  }

  /* 1px centres the 16px icon on the first 18px line. */
  .settings-note .nv-icon {
    flex: none;
    width: 16px;
    height: 16px;
    margin-top: 1px;
    color: var(--nv-text-3);
  }

  /* Deletion: a danger hairline on the card is enough; the red lives in the button. */
  .danger-zone {
    border-color: var(--nv-danger-line);
  }

  /* ---- Sessions ---------------------------------------------------------------- */
  .session-list {
    display: grid;
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .session-row {
    display: flex;
    align-items: center;
    gap: var(--nv-space-3);
    min-height: 64px;
    padding: var(--nv-space-2) 0;
  }

  .session-row + .session-row {
    border-top: 1px solid var(--nv-line-soft);
  }

  .session-icon {
    display: grid;
    flex: none;
    place-items: center;
    width: 24px;
    height: 24px;
    color: var(--nv-text-3);
  }

  .session-icon .nv-icon {
    width: 18px;
    height: 18px;
  }

  .session-text {
    display: grid;
    flex: 1;
    min-width: 0;
  }

  .session-text b {
    overflow: hidden;
    color: var(--nv-text);
    font-size: var(--nv-fs-14);
    font-weight: var(--nv-fw-medium);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .session-text span {
    color: var(--nv-text-3);
    font-size: var(--nv-fs-12);
  }

  .session-row .icon-btn .nv-icon {
    width: 16px;
    height: 16px;
  }

  /* ---- Aliases -------------------------------------------------------------------- */
  .aliases-card {
    gap: var(--nv-space-3);
  }

  .alias-meter {
    display: grid;
    gap: var(--nv-space-2);
  }

  .alias-meter small {
    color: var(--nv-text-3);
  }

  .alias-list {
    display: grid;
    gap: var(--nv-space-2);
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .alias-list:empty {
    display: none;
  }

  .alias-row {
    display: flex;
    align-items: center;
    gap: var(--nv-space-3);
    min-height: 56px;
    padding: var(--nv-space-2) var(--nv-space-3);
    border: 1px solid var(--nv-line);
    border-radius: var(--nv-r-md);
    background: var(--nv-surface-2);
  }

  .alias-row.is-inactive {
    border-style: dashed;
    background: transparent;
  }

  .alias-row.is-inactive .alias-text b {
    text-decoration: line-through;
  }

  .alias-row.is-inactive .alias-text :is(b, .nv-address-name) {
    color: var(--nv-text-3);
  }

  .alias-icon {
    display: grid;
    flex: none;
    place-items: center;
    width: 24px;
    height: 24px;
    color: var(--nv-text-3);
  }

  .alias-icon .nv-icon {
    width: 18px;
    height: 18px;
  }

  .alias-text {
    display: grid;
    flex: 1;
    gap: 2px;
    min-width: 0;
  }

  /* The address in the signature (.nv-address): mono, the host dim, the name bright. */
  .alias-text b {
    overflow: hidden;
    color: var(--nv-text);
    font-size: var(--nv-fs-14);
    font-weight: var(--nv-fw-medium);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .alias-text small {
    color: var(--nv-text-3);
  }

  .alias-actions {
    display: flex;
    flex: none;
    gap: var(--nv-space-1);
  }

  .alias-actions .icon-btn .nv-icon {
    width: 16px;
    height: 16px;
  }

  :is(.alias-actions, .session-row) .danger-icon:hover {
    background: var(--nv-danger-soft);
    color: var(--nv-danger);
  }

  .alias-form {
    display: grid;
    gap: var(--nv-space-2);
    margin-top: var(--nv-space-2);
  }

  .alias-form .input-group > button .nv-icon {
    width: 16px;
    height: 16px;
  }

  .alias-status {
    display: flex;
    align-items: center;
    gap: var(--nv-space-2);
    min-height: 20px;
    color: var(--nv-text-3);
    font-size: var(--nv-fs-13);
  }

  .alias-status .nv-icon {
    width: 16px;
    height: 16px;
  }

  .alias-status[data-kind="ok"] {
    color: var(--nv-success);
  }

  .alias-status[data-kind="bad"] {
    color: var(--nv-danger);
  }

  /* Release 163: free but paid 3–4 character address (dash-aliases.js paid()). */
  .alias-status[data-kind="paid"] {
    flex-wrap: wrap;
    gap: var(--nv-space-2);
  }

  .alias-status-note {
    flex-basis: 100%;
    font-size: var(--nv-fs-12);
    line-height: 1.5;
  }

  /* A neutral note; the crown is the Premium mark in its ink, the button carries the accent. */
  .alias-upsell {
    align-items: center;
  }

  .alias-upsell > .nv-icon {
    margin-top: 0;
    color: var(--nv-premium);
  }

  .alias-upsell > span {
    flex: 1;
  }

  /* ---- Dialog content ------------------------------------------------------------- */
  .mfa-setup {
    display: grid;
    grid-template-columns: 180px minmax(0, 1fr);
    gap: var(--nv-space-5);
    align-items: start;
  }

  /* The QR needs its white plate to scan. */
  .mfa-qr {
    padding: var(--nv-space-3);
    border-radius: var(--nv-r-md);
    background: #fff;
  }

  .mfa-qr svg {
    width: 100%;
    height: auto;
  }

  .mfa-steps {
    display: grid;
    gap: var(--nv-space-3);
  }

  .mfa-steps ol {
    display: grid;
    gap: var(--nv-space-2);
    padding-left: 1.2em;
    color: var(--nv-text-2);
    font-size: var(--nv-fs-13);
  }

  .mfa-steps ol li::marker {
    color: var(--nv-text-3);
    font-family: var(--nv-font-mono);
  }

  .mfa-steps .input-group input {
    font-size: var(--nv-fs-12);
  }

  .recovery-codes {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--nv-space-2);
    margin: 0;
    padding: var(--nv-space-4);
    border: 1px dashed var(--nv-line-strong);
    border-radius: var(--nv-r-md);
    background: var(--nv-bg-deep);
    list-style: none;
    color: var(--nv-text);
    font-size: var(--nv-fs-14);
    letter-spacing: .04em;
    text-align: center;
  }

  .delete-list {
    display: grid;
    gap: var(--nv-space-2);
    margin: 0;
    padding: var(--nv-space-3) var(--nv-space-4);
    border: 1px solid var(--nv-danger-line);
    border-radius: var(--nv-r-md);
    background: var(--nv-danger-soft);
    list-style: none;
  }

  .delete-list li {
    display: flex;
    align-items: center;
    gap: var(--nv-space-2);
    color: var(--nv-text);
    font-size: var(--nv-fs-13);
  }

  .delete-list .nv-icon {
    flex: none;
    width: 16px;
    height: 16px;
    color: var(--nv-danger);
  }

  #authDialog .auth-error {
    text-align: left;
  }

  /* ---- Responsive ------------------------------------------------------------------- */
  @media (max-width: 900px) {
    .settings-layout {
      grid-template-columns: minmax(0, 1fr);
      gap: var(--nv-space-4);
    }

    .settings-nav {
      position: static;
      display: flex;
      gap: var(--nv-space-1);
      margin: 0 calc(var(--nv-space-4) * -1);
      padding: 0 var(--nv-space-4) 2px;
      overflow-x: auto;
      border: 0;
      border-radius: 0;
      background: transparent;
      scrollbar-width: none;
      scroll-snap-type: x proximity;
      /* Snapping and scrollIntoView keep the page gutter instead of cutting the first tab. */
      scroll-padding-inline: var(--nv-space-4);
    }

    .settings-nav [role="tab"] {
      flex: none;
      min-height: var(--nv-hit);
      padding: 0 var(--nv-space-4);
      border: 1px solid var(--nv-line);
      border-radius: var(--nv-r-control);
      background: var(--nv-surface-1);
      scroll-snap-align: start;
    }

    .settings-nav [role="tab"][aria-selected="true"] {
      border-color: var(--nv-line-strong);
      background: var(--nv-surface-3);
      box-shadow: none;
    }
  }

  @media (max-width: 640px) {
    .settings-card {
      padding: var(--nv-space-4);
    }

    .settings-row {
      flex-wrap: wrap;
      gap: var(--nv-space-3);
    }

    .settings-row-text {
      flex: 1 1 150px;
    }

    /* On its own line the group may shrink to the row and wrap (Email: chip · Подтвердить · Изменить at 320). */
    .settings-row-action {
      flex: 0 1 auto;
      max-width: 100%;
      margin-left: auto;
    }

    .settings-identity {
      align-items: flex-start;
      gap: var(--nv-space-4);
    }

    .settings-identity .avatar.xl {
      --av: 56px;
    }

    .alias-row {
      flex-wrap: wrap;
    }

    .alias-text {
      flex: 1 1 calc(100% - 24px - var(--nv-space-2));
    }

    .alias-actions {
      margin-left: auto;
    }

    .mfa-setup {
      grid-template-columns: minmax(0, 1fr);
      justify-items: center;
    }

    .mfa-qr {
      width: 200px;
    }

    .recovery-codes {
      grid-template-columns: minmax(0, 1fr);
    }
  }

  /* ---- Phones: compact + calm (r160) -------------------------------------------------- */
  @media (max-width: 640px) {
    /* r181: «Настройки» is for screen readers only on phones: the one page-title rule in app-shell.css. */
    .settings-layout {
      gap: var(--nv-space-3);
    }

    /* r166 (G4): all six sections visible as a 3 × 2 grid (the scroller hid half of them and, after a
       deep link to «Опасная зона», the active one too). */
    .settings-nav {
      display: grid;
      grid-template-columns: repeat(3, minmax(0, 1fr));
      gap: var(--nv-space-2);
      margin: 0;
      padding: 0;
      overflow: visible;
    }

    .settings-nav [role="tab"] {
      justify-content: center;
      gap: var(--nv-space-2);
      min-height: var(--nv-hit);
      padding: var(--nv-space-1) var(--nv-space-2);
      font-size: var(--nv-fs-13);
      line-height: 1.2;
      text-align: center;
      white-space: normal;
    }

    .settings-nav [role="tab"] .nv-icon {
      display: none;
    }

    .settings-panel {
      gap: var(--nv-space-3);
    }

    .settings-card {
      padding: var(--nv-space-3) var(--nv-space-4);
    }

    .settings-card > .card-head {
      flex-wrap: wrap;
      margin-bottom: 0;
    }

    /* A head action keeps its one-line label and drops under the title when both do not fit, still on the
       right, over the rows' own action column. */
    .settings-card > .card-head > div {
      flex: 1 1 160px;
    }

    .settings-card > .card-head > :is(button, a) {
      flex: none;
      margin-left: auto;
    }

    .settings-card .card-head h2 {
      font-size: var(--nv-fs-16);
    }

    .settings-identity {
      align-items: center;
      gap: var(--nv-space-3);
    }

    .settings-identity .avatar.xl {
      --av: 48px;
    }

    .settings-identity h2 {
      font-size: var(--nv-fs-18);
    }

    .settings-identity p {
      font-size: var(--nv-fs-13);
    }

    .identity-chips {
      margin-top: var(--nv-space-2);
    }

    .settings-row {
      gap: var(--nv-space-1) var(--nv-space-3);
      min-height: 56px;
      padding: var(--nv-space-2) 0;
    }

    .settings-row-text span {
      font-size: var(--nv-fs-12);
    }

    .settings-note {
      margin-top: var(--nv-space-2);
      font-size: var(--nv-fs-12);
    }

    .session-row {
      min-height: 56px;
    }

    .alias-row {
      gap: var(--nv-space-2);
      min-height: 52px;
      padding: var(--nv-space-2) var(--nv-space-3);
    }
  }
}

/* ---- r180: phones ------------------------------------------------------------------------ */
@layer surfaces {
  /* A long name or email wraps inside the identity card instead of running off the screen. */
  .settings-identity > div {
    min-width: 0;
  }

  .settings-identity :is(h2, p) {
    overflow-wrap: anywhere;
  }

  @media (max-width: 640px) {
    /* Current device: «Это устройство» moves under the name instead of squeezing it
       (24px icon + 44px button + the gaps). */
    .session-row.is-current {
      flex-wrap: wrap;
      row-gap: var(--nv-space-1);
    }

    .session-row.is-current .session-text {
      flex: 1 1 calc(100% - 68px - 3 * var(--nv-space-3));
    }

    .session-row.is-current > .chip {
      order: 3;
      margin-left: calc(24px + var(--nv-space-3));
    }

    /* New address: prefix and field share the first line, a full-width «Добавить» goes under them. */
    .aliases-card .alias-form .input-group {
      flex-wrap: wrap;
      row-gap: var(--nv-space-1);
    }

    .aliases-card .alias-form .input-group > input {
      flex: 1 1 0;
      min-width: 0;
    }

    .aliases-card .alias-form .input-group > button {
      flex: 1 1 100%;
    }
  }

  /* A card's head action («Завершить другие») is a full touch target on touch screens. */
  @media (pointer: coarse) {
    .settings-card > .card-head > :is(button, a) {
      min-height: var(--nv-hit);
    }
  }

  /* 320px: two columns of tabs, so every label keeps its padding and one line. */
  @media (max-width: 360px) {
    .settings-nav {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }
  }
}
