:root {
    color-scheme: light;
    --bg: #f6f8fb;
    --surface: #ffffff;
    --surface-soft: #edf4f7;
    --ink: #20242d;
    --muted: #667085;
    --line: #d9e0e7;
    --teal: #16756f;
    --teal-dark: #0d5e59;
    --amber: #b56b14;
    --red: #b42318;
    --blue: #265da8;
    --green: #227a48;
    --shadow: 0 18px 45px rgba(18, 31, 43, 0.09);
}

* {
    box-sizing: border-box;
}

html {
    overflow-x: hidden;
}

body {
    margin: 0;
    min-width: 320px;
    width: 100%;
    overflow-x: hidden;
    background: var(--bg);
    color: var(--ink);
    font-family: Arial, Helvetica, sans-serif;
    line-height: 1.45;
}

button,
input,
textarea,
select {
    font: inherit;
    min-width: 0;
}

button {
    border: 0;
    cursor: pointer;
}

.app-shell {
    min-height: 100vh;
}

.topbar {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 16px;
    align-items: center;
    padding: 18px clamp(16px, 4vw, 36px);
    background: #ffffff;
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    z-index: 20;
}

.brand-mark {
    width: 46px;
    height: 46px;
    display: grid;
    place-items: center;
    border-radius: 8px;
    background: var(--teal);
    color: #ffffff;
    font-weight: 800;
}

h1,
h2,
p {
    margin: 0;
}

h1 {
    font-size: 1.25rem;
}

h2 {
    font-size: 1rem;
}

.topbar p,
.inline-state,
.meta-badge {
    color: var(--muted);
    font-size: 0.86rem;
}

.main-tabs {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.tab-button,
.secondary-button,
.primary-button,
.accent-button,
.primary-link,
.danger-button,
.icon-button {
    min-height: 38px;
    border-radius: 8px;
    padding: 9px 14px;
    color: var(--ink);
    background: var(--surface-soft);
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    white-space: nowrap;
    max-width: 100%;
}

.tab-button.active,
.primary-button {
    background: var(--teal);
    color: #ffffff;
}

.accent-button {
    background: var(--amber);
    color: #ffffff;
}

.danger-button {
    background: var(--red);
    color: #ffffff;
}

.secondary-button {
    border: 1px solid var(--line);
}

.icon-button {
    width: 38px;
    padding: 0;
    font-weight: 700;
}

main {
    padding: clamp(16px, 3vw, 32px);
}

.view {
    display: none;
}

.view.active {
    display: block;
}

.hidden {
    display: none !important;
}

.workspace-grid {
    display: grid;
    grid-template-columns: minmax(230px, 320px) minmax(0, 1fr);
    gap: 20px;
    align-items: start;
}

.list-panel,
.editor-panel,
.settings-panel,
.weight-chart-panel,
.plan-output,
.history-panel,
.install-shell {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.list-panel,
.editor-panel,
.settings-panel,
.weight-chart-panel,
.plan-output,
.history-panel {
    padding: 18px;
    min-width: 0;
}

.panel-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
}

.segmented-control {
    display: inline-flex;
    max-width: 100%;
    padding: 3px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #f8fafc;
    gap: 3px;
}

.segment-button {
    min-height: 34px;
    border-radius: 6px;
    padding: 7px 12px;
    background: transparent;
    color: var(--muted);
    white-space: nowrap;
}

.segment-button.active {
    background: var(--teal);
    color: #ffffff;
}

.dog-list,
.history-list {
    display: grid;
    gap: 10px;
}

.dog-item,
.history-card {
    width: 100%;
    text-align: left;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px;
    background: #ffffff;
}

.dog-item {
    display: flex;
    align-items: center;
    gap: 10px;
}

.dog-item.active {
    border-color: var(--teal);
    background: #eef8f6;
}

.dog-item-body {
    min-width: 0;
}

.dog-item-body strong,
.history-card strong {
    display: block;
    margin-bottom: 3px;
}

.dog-item-body span,
.history-card span {
    color: var(--muted);
    font-size: 0.86rem;
    overflow-wrap: anywhere;
}

.history-card {
    display: grid;
    gap: 12px;
}

.history-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.history-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.history-actions button {
    min-height: 34px;
    padding: 7px 10px;
}

.history-preview .structured-plan {
    gap: 10px;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 14px;
}

.animal-photo-field {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #f8fafc;
}

.animal-photo-editor {
    display: grid;
    justify-items: center;
    flex: 0 0 auto;
}

.animal-photo-adjuster {
    display: grid;
    grid-template-columns: 24px 64px 24px;
    grid-template-rows: 24px 64px 24px;
    gap: 2px;
    align-items: center;
    justify-items: center;
}

.animal-photo-field label {
    flex: 1;
    min-width: 0;
}

.animal-avatar {
    width: 40px;
    height: 40px;
    flex: 0 0 40px;
    display: grid;
    place-items: center;
    overflow: hidden;
    border-radius: 999px;
    background: var(--teal);
    color: #ffffff;
    font-size: 0.78rem;
    font-weight: 800;
    letter-spacing: 0;
}

.animal-avatar img {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: cover;
    object-position: var(--photo-x, 50%) var(--photo-y, 50%);
    transform: translate(var(--photo-offset-x, 0%), var(--photo-offset-y, 0%)) scale(var(--photo-scale, 1.35));
    transform-origin: center;
    transition: transform 120ms ease, object-position 120ms ease;
    will-change: transform;
    pointer-events: none;
    user-select: none;
}

.animal-photo-preview {
    width: 64px;
    height: 64px;
    flex-basis: 64px;
    grid-column: 2;
    grid-row: 2;
    cursor: default;
    touch-action: none;
}

.animal-photo-field.has-photo .animal-photo-preview {
    cursor: grab;
}

.animal-photo-preview.dragging {
    cursor: grabbing;
}

.photo-move-button {
    width: 24px;
    height: 24px;
    min-height: 24px;
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: #ffffff;
    color: var(--teal);
    font-size: 0.9rem;
    font-weight: 800;
    line-height: 1;
}

.photo-move-button:hover,
.photo-move-button:focus-visible {
    background: var(--surface-soft);
}

.photo-move-button:active {
    transform: translateY(1px);
}

.photo-move-up {
    grid-column: 2;
    grid-row: 1;
}

.photo-move-left {
    grid-column: 1;
    grid-row: 2;
}

.photo-move-right {
    grid-column: 3;
    grid-row: 2;
}

.photo-move-down {
    grid-column: 2;
    grid-row: 3;
}

.animal-photo-field:not(.has-photo) .photo-move-button {
    visibility: hidden;
    pointer-events: none;
}

label,
fieldset {
    display: grid;
    gap: 7px;
    color: var(--muted);
    font-size: 0.88rem;
}

input,
textarea,
select {
    width: 100%;
    min-height: 40px;
    font-size: 1rem;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 8px 10px;
    background: #ffffff;
    color: var(--ink);
}

textarea {
    resize: vertical;
}

input:focus,
textarea:focus,
select:focus {
    outline: 3px solid rgba(22, 117, 111, 0.18);
    border-color: var(--teal);
}

fieldset {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px;
}

legend {
    color: var(--ink);
    font-weight: 700;
}

.wide {
    grid-column: 1 / -1;
}

.mixed-only.hidden {
    display: none;
}

.check-line {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 40px;
    color: var(--ink);
}

.check-line input {
    width: 17px;
    height: 17px;
    min-height: auto;
    accent-color: var(--teal);
}

.chip-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 8px;
    align-items: stretch;
}

.chip,
.choice-card {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 9px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 12px;
    color: var(--ink);
    background: #ffffff;
    max-width: 100%;
    min-height: 44px;
    text-align: left;
    overflow-wrap: anywhere;
}

.chip input,
.choice-card input {
    width: 16px;
    height: 16px;
    flex: 0 0 16px;
    min-height: auto;
    accent-color: var(--teal);
}

.feeding-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
}

.weight-chart-summary {
    margin: -6px 0 12px;
    color: var(--muted);
    font-size: 0.9rem;
}

.form-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
}

.status-strip {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 12px;
    margin: 18px 0;
}

.status-strip > div {
    border-left: 4px solid var(--blue);
    background: #eef4ff;
    border-radius: 8px;
    padding: 12px;
}

.status-strip span {
    display: block;
    color: var(--muted);
    font-size: 0.82rem;
}

.status-strip strong {
    display: block;
    margin-top: 4px;
}

.status-strip.overweight > div {
    border-left-color: var(--red);
    background: #fff1f0;
}

.status-strip.underweight > div {
    border-left-color: var(--amber);
    background: #fff7ed;
}

.status-strip.ideal > div {
    border-left-color: var(--green);
    background: #eef8f1;
}

.plan-layout {
    display: grid;
    grid-template-columns: 1fr;
    gap: 18px;
}

.weight-chart-panel {
    margin: 18px 0;
}

.weight-chart {
    min-width: 0;
}

.weight-chart-scroll {
    overflow-x: auto;
    padding-bottom: 2px;
}

.weight-chart-frame {
    display: grid;
    grid-template-columns: 48px minmax(320px, 1fr);
    gap: 10px;
    min-width: 520px;
}

.weight-chart-scale {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 230px;
    color: var(--muted);
    font-size: 0.78rem;
    text-align: right;
    padding: 6px 0 32px;
}

.weight-chart-plot {
    position: relative;
    min-height: 230px;
    border-left: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
    background:
        linear-gradient(to top, rgba(217, 224, 231, 0.55) 1px, transparent 1px) 0 0 / 100% 25%;
    padding: 12px 12px 32px;
}

.weight-ideal-band {
    position: absolute;
    left: 12px;
    right: 12px;
    z-index: 0;
    display: flex;
    align-items: center;
    min-height: 20px;
    border-top: 1px dashed rgba(34, 122, 72, 0.72);
    border-bottom: 1px dashed rgba(34, 122, 72, 0.72);
    background: rgba(34, 122, 72, 0.09);
    color: var(--green);
    font-size: 0.76rem;
    font-weight: 700;
    pointer-events: none;
}

.weight-ideal-band span {
    margin-left: 8px;
    background: rgba(255, 255, 255, 0.86);
    border-radius: 6px;
    padding: 2px 6px;
}

.weight-bars {
    position: relative;
    z-index: 1;
    display: flex;
    align-items: flex-end;
    gap: 10px;
    height: 186px;
}

.weight-bar {
    flex: 1 0 54px;
    min-width: 0;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-end;
    gap: 7px;
}

.weight-bar-fill {
    min-height: 18px;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    border-radius: 7px 7px 0 0;
    background: var(--blue);
    color: #ffffff;
    box-shadow: 0 8px 18px rgba(38, 93, 168, 0.18);
    padding-top: 5px;
    transition: height 160ms ease;
}

.weight-bar-fill span {
    font-size: 0.76rem;
    font-weight: 700;
}

.weight-bar small {
    min-height: 20px;
    color: var(--muted);
    font-size: 0.74rem;
    text-align: center;
    white-space: nowrap;
}

.weight-bar.under .weight-bar-fill {
    background: var(--amber);
    box-shadow: 0 8px 18px rgba(181, 107, 20, 0.18);
}

.weight-bar.ok .weight-bar-fill {
    background: var(--green);
    box-shadow: 0 8px 18px rgba(34, 122, 72, 0.18);
}

.weight-bar.over .weight-bar-fill {
    background: var(--red);
    box-shadow: 0 8px 18px rgba(180, 35, 24, 0.16);
}

.weight-chart-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-top: 12px;
    color: var(--muted);
    font-size: 0.8rem;
}

.weight-chart-legend span {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.weight-chart-legend i {
    width: 10px;
    height: 10px;
    border-radius: 999px;
    background: var(--blue);
}

.weight-chart-legend i.ideal {
    border: 1px dashed var(--green);
    background: rgba(34, 122, 72, 0.12);
}

.weight-chart-legend i.under {
    background: var(--amber);
}

.weight-chart-legend i.ok {
    background: var(--green);
}

.weight-chart-legend i.over {
    background: var(--red);
}

.plan-view {
    margin: 0;
    min-height: 280px;
    background: #f8fafc;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 14px;
}

.plan-view.empty-state {
    display: grid;
    place-items: center;
    color: var(--muted);
}

.structured-plan {
    display: grid;
    gap: 16px;
}

.plan-intro {
    display: grid;
    gap: 6px;
    padding: 12px;
    border-left: 4px solid var(--teal);
    border-radius: 8px;
    background: #eef8f6;
}

.plan-intro p {
    margin: 4px 0 0;
    color: var(--text);
}

.plan-table-section {
    display: grid;
    gap: 8px;
}

.plan-table-section h3,
.plan-notes h3 {
    margin: 0;
    font-size: 1rem;
}

.table-scroll {
    overflow-x: auto;
}

.plan-table {
    width: 100%;
    min-width: 680px;
    border-collapse: collapse;
    background: #ffffff;
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
}

.plan-table th,
.plan-table td {
    padding: 10px 12px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: top;
}

.plan-table th {
    background: #edf2f7;
    color: var(--muted);
    font-weight: 700;
}

.plan-table tr:last-child td {
    border-bottom: 0;
}

.plan-notes ul {
    margin: 0;
    padding-left: 18px;
}

.structured-plan.compact .plan-intro {
    padding: 10px;
}

.structured-plan.compact .plan-table {
    min-width: 560px;
}

.settings-check {
    max-width: max-content;
}

.settings-stack {
    display: grid;
    gap: 20px;
    max-width: 1180px;
    margin: 0 auto;
}

.food-settings {
    display: grid;
    gap: 14px;
    margin: 12px 0 16px;
}

.food-group {
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
    min-width: 0;
}

.food-group h3 {
    margin: 0;
    padding: 11px 12px;
    background: var(--surface-soft);
    font-size: 0.96rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

.food-group h3 span {
    min-width: 0;
}

.food-group h3 small {
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 400;
}

.group-toggle {
    min-height: auto;
    font-size: 0.82rem;
    font-weight: 400;
}

.food-row {
    display: grid;
    grid-template-columns: minmax(220px, 1.35fr) repeat(3, minmax(110px, 0.8fr)) repeat(7, minmax(70px, 0.55fr)) minmax(130px, 1fr) minmax(86px, 0.45fr);
    gap: 8px;
    align-items: center;
    padding: 10px 12px;
    border-top: 1px solid var(--line);
}

.food-row label {
    color: var(--ink);
    min-width: 0;
}

.food-row .check-line span {
    min-width: 0;
}

.food-row .check-line strong,
.food-row .check-line small {
    display: block;
    overflow-wrap: anywhere;
}

.food-row .check-line small {
    margin-top: 2px;
    color: var(--muted);
    font-size: 0.76rem;
    font-weight: 400;
}

.food-row .check-line {
    min-width: 0;
}

.food-row input[type="number"] {
    min-width: 0;
}

.food-settings-empty {
    border: 1px dashed var(--line);
    border-radius: 8px;
    padding: 14px;
    color: var(--muted);
    background: #f8fafc;
}

.nutrition-status {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 34px;
    border-radius: 8px;
    background: #f8fafc;
    color: var(--muted);
    font-size: 0.78rem;
    text-align: center;
}

.barcode-result {
    margin-top: 14px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #f8fafc;
    padding: 12px;
    color: var(--ink);
}

.barcode-result strong,
.barcode-result span {
    display: block;
    overflow-wrap: anywhere;
}

.barcode-result span {
    margin-top: 4px;
    color: var(--muted);
    font-size: 0.86rem;
}

.food-group-disabled .food-row {
    opacity: 0.56;
}

.food-group-disabled .food-row input,
.food-group-disabled .food-row select {
    background: #f8fafc;
}

.compact-form {
    display: grid;
    gap: 12px;
    margin-top: 12px;
}

.ai-settings-entry {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 60;
    display: grid;
    place-items: center;
    padding: 18px;
    background: rgba(19, 29, 39, 0.52);
}

.modal-overlay.hidden {
    display: none;
}

.modal-panel {
    width: min(560px, 100%);
    max-height: min(760px, calc(100vh - 36px));
    overflow: auto;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
    padding: 20px;
}

.confirm-panel {
    width: min(460px, 100%);
}

.scanner-panel {
    width: min(520px, 100%);
}

.scanner-video {
    width: 100%;
    aspect-ratio: 4 / 3;
    display: block;
    border-radius: 8px;
    background: #111827;
    object-fit: cover;
}

.confirm-text {
    color: var(--ink);
    margin-bottom: 18px;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    flex-wrap: wrap;
}

.toast {
    position: fixed;
    right: 18px;
    bottom: 18px;
    max-width: min(420px, calc(100vw - 36px));
    background: var(--ink);
    color: #ffffff;
    border-radius: 8px;
    padding: 12px 14px;
    opacity: 0;
    transform: translateY(12px);
    pointer-events: none;
    transition: opacity 180ms ease, transform 180ms ease;
    z-index: 50;
}

.toast.show {
    opacity: 1;
    transform: translateY(0);
}

.notice {
    border-radius: 8px;
    padding: 12px;
    margin: 16px 0;
}

.notice.success {
    background: #eef8f1;
    color: var(--green);
}

.notice.danger {
    background: #fff1f0;
    color: var(--red);
}

.install-page {
    display: grid;
    place-items: center;
    min-height: 100vh;
    padding: 18px;
}

.install-shell {
    width: min(620px, 100%);
    padding: 28px;
}

.install-shell form {
    margin-top: 18px;
}

@media (max-width: 1100px) {
    .form-grid,
    .status-strip {
        grid-template-columns: 1fr 1fr;
    }

    .plan-output,
    .history-panel {
        grid-column: 1 / -1;
    }

    .food-row {
        grid-template-columns: minmax(170px, 1fr) repeat(3, minmax(80px, 1fr));
    }
}

@media (max-width: 760px) {
    body {
        padding-bottom: 74px;
    }

    .topbar {
        grid-template-columns: auto 1fr;
    }

    .main-tabs {
        position: fixed;
        left: 10px;
        right: 10px;
        bottom: max(10px, env(safe-area-inset-bottom));
        z-index: 55;
        width: auto;
        padding: 8px;
        border: 1px solid var(--line);
        border-radius: 8px;
        background: rgba(255, 255, 255, 0.96);
        box-shadow: var(--shadow);
    }

    .tab-button {
        flex: 1;
    }

    .tab-button.active {
        display: none;
    }

    .workspace-grid,
    .form-grid,
    .status-strip,
    .plan-layout {
        grid-template-columns: 1fr;
    }

    .ai-settings-entry,
    .modal-actions {
        align-items: stretch;
        flex-direction: column;
    }

    .food-row {
        grid-template-columns: 1fr;
    }

    .chip-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .feeding-grid {
        grid-template-columns: 1fr;
    }

    .form-actions {
        align-items: stretch;
    }

    .form-actions > button {
        width: 100%;
    }

    .history-card-header {
        align-items: stretch;
        flex-direction: column;
    }

    .history-actions {
        width: 100%;
    }

    .history-actions button {
        flex: 1;
    }

    .segmented-control {
        display: flex;
        width: 100%;
    }

    .segment-button {
        flex: 1;
    }
}

@media (max-width: 520px) {
    .topbar {
        gap: 10px;
        padding: 12px;
    }

    .brand-mark {
        width: 40px;
        height: 40px;
    }

    h1 {
        font-size: 1.08rem;
    }

    main {
        padding: 10px;
    }

    .chip-grid {
        grid-template-columns: 1fr;
    }

    .list-panel,
    .editor-panel,
    .settings-panel,
    .weight-chart-panel,
    .plan-output,
    .history-panel,
    .modal-panel {
        padding: 12px;
    }

    .tab-button,
    .secondary-button,
    .primary-button,
    .accent-button,
    .primary-link,
    .danger-button,
    .icon-button {
        white-space: normal;
    }

    .status-strip {
        gap: 8px;
    }

    .toast {
        right: 10px;
        bottom: 10px;
        max-width: calc(100vw - 20px);
    }
}
