/* Buttons: charcoal is the main action; outlined is secondary; quiet is for low-emphasis links. */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: var(--control-height);
    padding: 7px 14px;
    border: 1px solid var(--ink);
    border-radius: var(--radius-sm);
    background: var(--ink);
    color: #fff;
    font: inherit;
    font-size: 14px;
    font-weight: 500;
    line-height: 1.2;
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
    transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease;
}

.btn:hover:not(:disabled) { background: #3a413b; border-color: #3a413b; color: #fff; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled,
.btn[aria-disabled="true"] { cursor: not-allowed; opacity: 0.45; }

.btn-secondary,
.btn.secondary {
    border-color: var(--line-strong);
    background: var(--surface);
    color: var(--ink);
}

.btn-secondary:hover:not(:disabled),
.btn.secondary:hover:not(:disabled) { border-color: var(--control-line); background: var(--surface-soft); color: var(--ink); }

.btn-quiet {
    border-color: transparent;
    background: transparent;
    color: var(--ink-2);
}

.btn-quiet:hover:not(:disabled) { border-color: transparent; background: var(--surface-muted); color: var(--ink); }

.btn-danger,
.btn.danger { border-color: var(--danger); background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled),
.btn.danger:hover:not(:disabled) { border-color: #7c2323; background: #7c2323; }

.btn-danger-outline,
.btn.danger-outline { border-color: var(--danger-line); background: var(--surface); color: var(--danger); }
.btn-danger-outline:hover:not(:disabled),
.btn.danger-outline:hover:not(:disabled) { border-color: var(--danger); background: var(--danger-soft); color: var(--danger); }

.btn.gold { border-color: var(--ink); background: var(--ink); color: #fff; }

.btn-sm,
.btn.compact { min-height: var(--control-height-sm); padding: 5px 10px; font-size: 13px; }
.btn-block { width: 100%; }
.btn .icon { margin-inline: -2px; }

.link-button {
    padding: 0;
    border: 0;
    background: none;
    color: var(--ink);
    font: inherit;
    font-weight: 500;
    text-decoration: underline;
    text-underline-offset: 3px;
    cursor: pointer;
}

/* Form fields */
.field { display: grid; gap: 6px; min-width: 0; align-content: start; }
.field > label,
label {
    display: block;
    margin: 0;
    color: var(--ink);
    font-size: 13px;
    font-weight: 500;
    line-height: 1.35;
}

label .muted,
.optional { color: var(--muted); font-weight: 400; }

input,
select,
textarea {
    width: 100%;
    min-height: var(--control-height);
    padding: 8px 10px;
    border: 1px solid var(--control-line);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--ink);
    font: inherit;
    font-size: 14px;
    caret-color: var(--ink);
}

select {
    padding-right: 32px;
    appearance: none;
    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='%23646962' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
    background-position: right 10px center;
    background-repeat: no-repeat;
}

input:hover:not(:disabled):not([readonly]),
select:hover:not(:disabled),
textarea:hover:not(:disabled):not([readonly]) { border-color: var(--ink-2); }

input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    border-color: var(--ink);
    outline: 2px solid rgba(133, 105, 43, 0.35);
    outline-offset: 0;
}

input::placeholder,
textarea::placeholder { color: #80857e; opacity: 1; }

input[readonly],
textarea[readonly],
input:disabled,
select:disabled,
textarea:disabled {
    border-color: var(--line-strong);
    background: var(--surface-soft);
    color: var(--muted);
}

input:disabled,
select:disabled,
textarea:disabled { cursor: not-allowed; }

textarea { min-height: 96px; resize: vertical; line-height: 1.5; }

input[type="checkbox"],
input[type="radio"] {
    flex: 0 0 auto;
    width: 16px;
    height: 16px;
    min-height: 0;
    margin: 0;
    padding: 0;
    accent-color: var(--ink);
}

input[type="file"] { padding: 6px; background: var(--surface-soft); }
input[type="file"]::file-selector-button {
    margin-right: 10px;
    padding: 5px 10px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--ink);
    font: inherit;
    font-size: 13px;
    cursor: pointer;
}

[aria-invalid="true"] { border-color: var(--danger) !important; }
.field-error { margin: 4px 0 0; color: var(--danger); font-size: 13px; line-height: 1.45; }

.check,
.checkbox-label {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    margin: 0;
    color: var(--ink);
    font-size: 14px;
    font-weight: 400;
    line-height: 1.45;
    cursor: pointer;
}

.check input,
.checkbox-label input { margin-top: 2px; }
.check-text { display: grid; gap: 2px; }
.check-text .hint { margin: 0; }

.form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-4) var(--space-4);
}

.form-grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.form-grid .span-all,
.full-span { grid-column: 1 / -1; }

.form-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: var(--space-2);
    margin-top: var(--space-5);
}

.form-actions.start { justify-content: flex-start; }

/* Status chips. Tones come from config('portal.status_classes'). */
.chip,
.status {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 22px;
    padding: 2px 8px;
    border-radius: var(--radius-sm);
    background: var(--surface-muted);
    color: var(--ink-2);
    font-size: 12px;
    font-weight: 600;
    line-height: 1.3;
    white-space: nowrap;
    vertical-align: middle;
}

.chip::before,
.status::before {
    content: '';
    flex: 0 0 auto;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
    opacity: 0.85;
}

.chip.ok, .status.ok { background: var(--ok-soft); color: var(--ok); }
.chip.warn, .status.warn { background: var(--warn-soft); color: var(--warn); }
.chip.danger, .status.danger { background: var(--danger-soft); color: var(--danger); }
.chip.info, .status.info { background: var(--info-soft); color: var(--info); }
.chip.neutral, .status.neutral { background: var(--surface-muted); color: var(--ink-2); }
.chip.plain::before { display: none; }

/* Panels */
.panel {
    margin-bottom: var(--space-5);
    padding: var(--space-5);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
}

.panel > :last-child { margin-bottom: 0; }
.panel-flush { padding: 0; overflow: hidden; }

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: var(--space-2) var(--space-4);
    margin-bottom: var(--space-4);
}

.panel-flush > .panel-header {
    margin: 0;
    padding: var(--space-4) var(--space-5);
    border-bottom: 1px solid var(--line);
}

.panel-header p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.panel-body { padding: var(--space-5); }
.panel-footer {
    padding: var(--space-3) var(--space-5);
    border-top: 1px solid var(--line);
    background: var(--surface-soft);
}

.panel-link {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    color: var(--ink-2);
    font-size: 13px;
    font-weight: 500;
    text-decoration: none;
}

.panel-link:hover { color: var(--ink); text-decoration: underline; }

.section-divider { margin: var(--space-5) 0; border: 0; border-top: 1px solid var(--line); }
.section-gap { margin-top: var(--space-5); }

/* Label and value pairs */
.detail-list {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-4) var(--space-6);
    margin: 0;
}

.detail-list > div { min-width: 0; }
.detail-list dt { margin-bottom: 2px; color: var(--muted); font-size: 13px; }
.detail-list dd { margin: 0; color: var(--ink); overflow-wrap: anywhere; }
.detail-list .span-all { grid-column: 1 / -1; }
.detail-list-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }

.summary-list { display: grid; margin: 0; }
.summary-list > div {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-4);
    padding: 9px 0;
    border-bottom: 1px solid var(--line);
}
.summary-list > div:first-child { padding-top: 0; }
.summary-list > div:last-child { padding-bottom: 0; border-bottom: 0; }
.summary-list dt { color: var(--muted); font-size: 13px; }
.summary-list dd { margin: 0; font-weight: 500; text-align: right; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.summary-list .is-total dd { font-size: 15px; font-weight: 600; }

/* Legacy definition grid used by screens not yet moved to detail-list */
.definition-grid { display: grid; gap: var(--space-4) var(--space-6); }
.definition-grid > div { min-width: 0; overflow-wrap: anywhere; }
.definition-grid strong { display: block; margin-bottom: 2px; color: var(--muted); font-size: 13px; font-weight: 400; }

/* Summary figures */
.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: var(--space-3);
    margin-bottom: var(--space-5);
}

.stat {
    display: grid;
    gap: 4px;
    align-content: start;
    min-width: 0;
    padding: var(--space-4);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    color: inherit;
    text-decoration: none;
}

a.stat:hover { border-color: var(--line-strong); background: var(--surface-soft); }
.stat-label { color: var(--muted); font-size: 13px; }
.stat-value { font-size: 24px; font-weight: 600; line-height: 1.2; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.stat-note { color: var(--muted); font-size: 12px; }

/* Activity timeline */
.timeline { position: relative; display: grid; gap: var(--space-4); margin: 0; padding: 0; list-style: none; }
.timeline-item { position: relative; display: grid; grid-template-columns: 16px minmax(0, 1fr); gap: var(--space-3); }
.timeline-item::before {
    content: '';
    position: absolute;
    left: 7px;
    top: 18px;
    bottom: calc(-1 * var(--space-4));
    width: 1px;
    background: var(--line);
}
.timeline-item:last-child::before { display: none; }
.timeline-dot {
    width: 9px;
    height: 9px;
    margin: 6px 0 0 3px;
    border-radius: 50%;
    background: var(--line-strong);
    box-shadow: 0 0 0 3px var(--surface);
}
.timeline-dot.ok { background: var(--ok); }
.timeline-dot.warn { background: #b88a1e; }
.timeline-dot.danger { background: var(--danger); }
.timeline-dot.info { background: var(--info); }
.timeline-head { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 10px; }
.timeline-title { font-weight: 600; }
.timeline-time { color: var(--muted); font-size: 13px; }
.timeline-body { margin: 4px 0 0; color: var(--ink-2); white-space: pre-line; }

/* Referral progress */
.progress-steps {
    display: grid;
    grid-template-columns: repeat(var(--steps, 5), minmax(0, 1fr));
    gap: 6px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.progress-step { display: grid; gap: 8px; min-width: 0; color: var(--muted); font-size: 13px; }
.progress-step::before { content: ''; height: 4px; border-radius: 2px; background: var(--line); }
.progress-step.is-done { color: var(--ink-2); }
.progress-step.is-done::before { background: var(--ink-2); }
.progress-step.is-current { color: var(--ink); font-weight: 600; }
.progress-step.is-current::before { background: var(--gold); }
.progress-step.is-stopped::before { background: var(--danger); }
.progress-phone-label { display: none; }

/* Disclosure sections */
.disclosure > summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    list-style: none;
    cursor: pointer;
}

.disclosure > summary::-webkit-details-marker { display: none; }
.disclosure > summary .disclosure-icon { color: var(--muted); transition: transform 150ms ease; }
.disclosure[open] > summary .disclosure-icon { transform: rotate(180deg); }

/* Confirmation dialog */
.dialog {
    width: min(460px, calc(100vw - 32px));
    padding: 0;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--ink);
    box-shadow: var(--shadow-float);
}

.dialog::backdrop { background: rgba(31, 36, 32, 0.45); }
.dialog-body { display: grid; gap: var(--space-3); padding: var(--space-5) var(--space-5) var(--space-4); }
.dialog-body h2 { font-size: 18px; }
.dialog-body > p { margin: 0; }
.dialog-security { display: grid; gap: var(--space-2); padding-top: var(--space-3); border-top: 1px solid var(--line); }
.dialog-security .btn { justify-self: start; }
.dialog-security p { margin: 0; }
.dialog-actions {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-5);
    border-top: 1px solid var(--line);
    background: var(--surface-soft);
}

/* Legacy layout helpers still used by screens not yet migrated */
.grid { display: grid; gap: var(--space-3); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.alert {
    margin-bottom: var(--space-4);
    padding: var(--space-3) var(--space-4);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
}
.alert.error { border-color: var(--danger-line); background: var(--danger-soft); color: #5c1c1c; }
.alert ul { margin: 6px 0 0; padding-left: 18px; }
.subsection-heading { margin-bottom: var(--space-3); }
.subsection-heading p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

@media (max-width: 900px) {
    .detail-list-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .grid-3, .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 640px) {
    input, select, textarea { font-size: 16px; }
    .form-grid,
    .form-grid-3,
    .detail-list,
    .detail-list-3,
    .grid-2, .grid-3, .grid-4 { grid-template-columns: minmax(0, 1fr); }
    .panel { padding: var(--space-4); margin-bottom: var(--space-4); }
    .panel-flush > .panel-header { padding: var(--space-3) var(--space-4); }
    .panel-body { padding: var(--space-4); }
    .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-2); }
    .stat { padding: var(--space-3); }
    .stat-value { font-size: 20px; }
    .form-actions { justify-content: stretch; }
    .form-actions > .btn,
    .form-actions > form { flex: 1 1 auto; }
    .form-actions > form .btn { width: 100%; }
    .progress-step,
    .progress-step.is-current { font-size: 0; gap: 0; }
    .progress-phone-label { display: block; margin: var(--space-2) 0 0; color: var(--ink-2); font-size: 13px; }
}

@media (pointer: coarse) {
    .btn, .btn-sm, .btn.compact,
    input:not([type="checkbox"]):not([type="radio"]), select { min-height: var(--touch); }
    input[type="checkbox"], input[type="radio"] { width: 18px; height: 18px; }
}

/* Form layouts */
.stack-form { display: grid; gap: var(--space-4); }
.stack-form > .form-actions { margin-top: var(--space-1); }
.narrow-layout { max-width: 640px; }
.inline-field-form { display: flex; align-items: flex-end; flex-wrap: wrap; gap: var(--space-3); }
.inline-field-form > .field { flex: 1 1 280px; }
.side-links { display: grid; gap: var(--space-2); margin-top: var(--space-4); padding-top: var(--space-3); border-top: 1px solid var(--line); }
.row-actions { display: inline-flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: 6px; }
.row-actions form { margin: 0; }
.notice > form,
.notice > .btn { margin-left: auto; flex: 0 0 auto; }
.notice.compact { margin-bottom: 0; padding: var(--space-2) var(--space-3); font-size: 13px; }
.notice.compact > .icon { margin-top: 2px; }
