/* =============================================================================
   FORMS - inputs, selects, validation, searchable dropdowns
   ============================================================================= */

.form-group { margin-bottom: 1rem; }

.form-label {
    display: block;
    margin-bottom: .35rem;
    font-size: .85rem;
    font-weight: 600;
    color: var(--text-soft);
}

.form-label .required { color: var(--red-500); margin-left: 2px; }

.form-hint {
    display: block;
    margin-top: .28rem;
    font-size: .78rem;
    color: var(--muted);
}

.form-control,
.form-select,
textarea.form-control {
    display: block;
    width: 100%;
    padding: .55rem .8rem;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font-family: inherit;
    font-size: .9rem;
    line-height: 1.5;
    transition: border-color var(--transition), box-shadow var(--transition), background var(--transition);
    -webkit-appearance: none;
    appearance: none;
}

.form-control::placeholder { color: var(--muted-light); }

.form-control:focus,
.form-select:focus,
textarea.form-control:focus {
    outline: none;
    border-color: var(--brand-500);
    box-shadow: 0 0 0 3px rgba(23, 100, 216, .10);
}

.form-control:disabled,
.form-select:disabled,
.form-control[readonly] {
    background: var(--bg-soft);
    color: var(--muted);
    cursor: not-allowed;
}

textarea.form-control { resize: vertical; min-height: 84px; }

.form-select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%236b7f99' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right .75rem center;
    padding-right: 2.1rem;
}

.form-control-sm, .form-select-sm { padding: .35rem .6rem; font-size: .84rem; }
.form-control-lg { padding: .72rem 1rem; font-size: 1rem; }

.form-control.is-invalid,
.form-select.is-invalid {
    border-color: #e89292;
    background-color: #fffafa;
}
.form-control.is-invalid:focus,
.form-select.is-invalid:focus { box-shadow: 0 0 0 3px var(--red-50); }

.invalid-feedback {
    display: block;
    margin-top: .28rem;
    font-size: .78rem;
    color: var(--red-500);
    font-weight: 500;
}

/* Input groups */
.input-group {
    display: flex;
    align-items: stretch;
    width: 100%;
}

.input-group > .form-control,
.input-group > .form-select { flex: 1 1 auto; min-width: 0; }

.input-group-text {
    display: flex;
    align-items: center;
    padding: .55rem .75rem;
    background: var(--surface-tint);
    border: 1px solid var(--border-strong);
    color: var(--muted);
    font-size: .86rem;
    font-weight: 600;
    white-space: nowrap;
}

.input-group > :first-child { border-radius: var(--radius-sm) 0 0 var(--radius-sm); }
.input-group > :last-child  { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }
.input-group > :not(:first-child) { margin-left: -1px; }
.input-group > :not(:first-child):not(:last-child) { border-radius: 0; }

/* Checkbox / radio */
.form-check {
    display: flex;
    align-items: center;
    gap: .5rem;
    margin-bottom: .45rem;
    cursor: pointer;
}

.form-check input[type="checkbox"],
.form-check input[type="radio"] {
    width: 17px;
    height: 17px;
    accent-color: var(--brand-500);
    cursor: pointer;
    flex-shrink: 0;
    margin: 0;
}

.form-check-label {
    font-size: .88rem;
    color: var(--text-soft);
    user-select: none;
    cursor: pointer;
}

/* Switch */
.form-switch {
    display: inline-flex;
    align-items: center;
    gap: .6rem;
    cursor: pointer;
}

.form-switch input { display: none; }

.form-switch-track {
    width: 42px;
    height: 23px;
    border-radius: 999px;
    background: var(--border-strong);
    position: relative;
    transition: background var(--transition);
    flex-shrink: 0;
}

.form-switch-track::after {
    content: '';
    position: absolute;
    top: 2.5px;
    left: 2.5px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #fff;
    box-shadow: var(--shadow-xs);
    transition: transform var(--transition);
}

.form-switch input:checked + .form-switch-track { background: var(--brand-500); }
.form-switch input:checked + .form-switch-track::after { transform: translateX(19px); }

/* Two-column responsive form grid */
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 0 1.1rem;
}

.form-grid .span-2 { grid-column: span 2; }
.form-grid .span-full { grid-column: 1 / -1; }

@media (max-width: 640px) {
    .form-grid { grid-template-columns: 1fr; }
    .form-grid .span-2 { grid-column: span 1; }
}

.form-section-title {
    font-size: .78rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .07em;
    color: var(--brand-600);
    padding-bottom: .4rem;
    border-bottom: 1px solid var(--border);
    margin: 1.4rem 0 1rem;
    grid-column: 1 / -1;
}
.form-section-title:first-child { margin-top: 0; }

/* -----------------------------------------------------------------------------
   Searchable select (custom component driven by app.js)
   -------------------------------------------------------------------------- */
.searchable-select { position: relative; }

.searchable-select-input {
    width: 100%;
    padding: .55rem 2rem .55rem .8rem;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    font-family: inherit;
    font-size: .9rem;
    color: var(--text);
    cursor: pointer;
}
.searchable-select-input:focus {
    outline: none;
    border-color: var(--brand-400);
    box-shadow: 0 0 0 3px var(--brand-50);
}
.searchable-select-input::placeholder { color: var(--muted-light); }

.searchable-select-caret {
    position: absolute;
    right: .7rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--muted);
    pointer-events: none;
}

.searchable-select-menu {
    position: absolute;
    top: calc(100% + 5px);
    left: 0;
    right: 0;
    max-height: 260px;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    z-index: 1050;
    display: none;
    padding: .3rem;
}
.searchable-select.open .searchable-select-menu { display: block; }

.searchable-select-option {
    padding: .45rem .65rem;
    border-radius: 6px;
    font-size: .88rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .5rem;
}
.searchable-select-option:hover,
.searchable-select-option.highlighted { background: var(--brand-50); color: var(--brand-700); }
.searchable-select-option.selected { background: var(--brand-50); font-weight: 600; color: var(--brand-700); }
.searchable-select-option small { color: var(--muted); font-size: .78rem; }
.searchable-select-empty { padding: .8rem; text-align: center; color: var(--muted); font-size: .85rem; }

/* Read-only computed field */
.form-control.computed {
    background: var(--brand-50);
    border-color: var(--brand-100);
    font-weight: 600;
    color: var(--brand-700);
}

/* -----------------------------------------------------------------------------
   Remote medicine picker (looks medicines up over AJAX)
   Used instead of a <select> so the page does not embed the whole catalogue.
   -------------------------------------------------------------------------- */
.medicine-picker { position: relative; }

.medicine-picker-input {
    width: 100%;
    padding: .42rem .6rem;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    font-family: inherit;
    font-size: .86rem;
    color: var(--text);
    transition: border-color var(--transition), box-shadow var(--transition);
}

.medicine-picker-input:focus {
    outline: none;
    border-color: var(--brand-400);
    box-shadow: 0 0 0 3px var(--brand-50);
}

.medicine-picker-input::placeholder { color: var(--muted-light); }
.medicine-picker-input.is-invalid { border-color: #e89292; background: #fffafa; }

.medicine-picker-menu {
    position: absolute;
    top: calc(100% + 4px);
    /* Align exactly with the input. The old min-width: 340px made the menu wider
       than a narrow picker, so it stuck out past the card edge and got clipped
       sideways (and pushed the page into horizontal scroll on a phone). */
    left: 0;
    right: 0;
    min-width: 0;
    max-width: 100%;
    max-height: 280px;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    z-index: 1200;
    display: none;
    padding: .3rem;
}

.medicine-picker-menu.open { display: block; }

.medicine-picker-option {
    padding: .45rem .6rem;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: .1rem;
    min-width: 0;
}

.medicine-picker-option:hover,
.medicine-picker-option.highlighted { background: var(--brand-50); }

/* Long names/generics truncate instead of pushing the menu wider than the card. */
.medicine-picker-name {
    font-size: .86rem;
    font-weight: 600;
    color: var(--text);
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.medicine-picker-meta {
    display: flex;
    gap: .6rem;
    font-size: .74rem;
    color: var(--muted);
    flex-wrap: wrap;
    min-width: 0;
}

.medicine-picker-meta > span {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.medicine-picker-empty {
    padding: .8rem;
    text-align: center;
    color: var(--muted);
    font-size: .84rem;
}

/* Inside a dense table row the menu must escape the cell, so there it keeps its
   own width instead of shrinking to the input. */
.table-wrap .medicine-picker-menu {
    right: auto;
    min-width: 320px;
    max-width: 92vw;
}

/* A card that hosts a picker must not clip that picker's dropdown. .card carries
   overflow:hidden to trim its own rounded corners, which also cut the menu off
   at the card's bottom edge - on the alternatives page only the top slice of a
   15-row list stayed visible. Opt in with .card-dropdown and let the tinted
   first/last row carry the corner radius instead of the card. */
.card-dropdown { overflow: visible; }
.card-dropdown > .card-header { border-radius: var(--radius) var(--radius) 0 0; }
.card-dropdown > .card-footer,
.card-dropdown > form > .card-footer { border-radius: 0 0 var(--radius) var(--radius); }
