* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: "Segoe UI", "PingFang SC", sans-serif;
    background: #f5f6f8;
    color: #111827;
}

body.auth-page {
    min-height: 100vh;
    background:
        radial-gradient(circle at top left, rgba(34, 197, 94, 0.12), transparent 30%),
        linear-gradient(180deg, #f7faf7 0%, #eef2f7 100%);
}

a {
    color: inherit;
    text-decoration: none;
}

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

.sidebar {
    width: 272px;
    background: rgba(250, 250, 250, 0.95);
    border-right: 1px solid #e5e7eb;
    padding: 26px 18px;
}

.brand {
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 1px solid #e5e7eb;
}

.brand-row {
    display: flex;
    align-items: center;
    gap: 12px;
}

.brand-mark {
    width: 32px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    background: #f0fdf4;
    color: #166534;
    flex: 0 0 32px;
}

.brand-mark svg {
    width: 22px;
    height: 22px;
    display: block;
}

.brand h1 {
    margin: 0;
    font-size: 17px;
}

.brand p {
    margin: 6px 0 0;
    font-size: 11px;
    color: #6b7280;
    letter-spacing: 0.14em;
    text-transform: uppercase;
}

.nav-link {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-radius: 10px;
    color: #4b5563;
    margin-bottom: 8px;
    font-size: 16px;
    font-weight: 600;
    line-height: 1.35;
}

.nav-icon {
    width: 20px;
    height: 20px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 20px;
}

.nav-icon svg {
    width: 20px;
    height: 20px;
    display: block;
}

.nav-link.active,
.nav-link:hover {
    color: #111827;
    background: #f0f2f5;
}

.main {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.topbar {
    height: 56px;
    border-bottom: 1px solid #e5e7eb;
    background: rgba(255, 255, 255, 0.9);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
}

.page {
    padding: 24px;
}

.auth-shell {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.auth-card {
    width: min(420px, 100%);
    background: rgba(255, 255, 255, 0.96);
    border: 1px solid #e5e7eb;
    border-radius: 20px;
    box-shadow: 0 24px 60px rgba(15, 23, 42, 0.10);
    padding: 28px;
}

.auth-brand {
    margin-bottom: 18px;
}

.auth-brand h1 {
    margin: 0;
    font-size: 28px;
}

.auth-brand p {
    margin: 8px 0 0;
    color: #6b7280;
}

.auth-form {
    display: grid;
    gap: 14px;
}

.cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 14px;
    margin-bottom: 20px;
}

.card,
.panel {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 14px;
    box-shadow: 0 6px 20px rgba(15, 23, 42, 0.04);
}

.card {
    padding: 16px;
}

.card small {
    color: #6b7280;
}

.card strong {
    display: block;
    margin-top: 8px;
    font-size: 28px;
}

.panel {
    padding: 18px;
    margin-bottom: 18px;
}

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

.grid-2,
.grid-3,
.grid-4 {
    display: grid;
    gap: 14px;
}

.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)); }

label {
    display: block;
    font-size: 13px;
    color: #4b5563;
    margin-bottom: 6px;
}

input,
select,
textarea,
button {
    width: 100%;
    border-radius: 10px;
    border: 1px solid #d1d5db;
    background: #fff;
    padding: 10px 12px;
    font-size: 14px;
}

textarea {
    min-height: 110px;
    resize: vertical;
}

button,
.btn {
    cursor: pointer;
    border: 0;
    background: #111827;
    color: #fff;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.btn-secondary {
    background: #f3f4f6;
    color: #111827;
    border: 1px solid #d1d5db;
}

.inline-form {
    display: flex;
    gap: 10px;
    align-items: end;
    flex-wrap: wrap;
}

.toolbar-btn {
    width: auto;
    min-width: 132px;
    height: 42px;
    padding: 0 16px;
    border-radius: 12px;
    white-space: nowrap;
}

.table-wrap {
    overflow: auto;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
}

table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
}

th,
td {
    text-align: left;
    padding: 12px 14px;
    border-bottom: 1px solid #f1f5f9;
    vertical-align: top;
    font-size: 14px;
}

th {
    color: #6b7280;
    background: #f8fafc;
    font-weight: 700;
}

.badge {
    display: inline-flex;
    align-items: center;
    padding: 4px 10px;
    border-radius: 999px;
    font-size: 12px;
    background: #eef2ff;
    color: #3730a3;
    white-space: nowrap;
}

.badge.warn { background: #fff7ed; color: #c2410c; }
.badge.success { background: #ecfdf5; color: #047857; }
.badge.muted { background: #f3f4f6; color: #4b5563; }
.badge.danger { background: #fef2f2; color: #b91c1c; }

.split {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 18px;
}

.formula-list {
    margin: 0;
    padding-left: 18px;
}

.muted {
    color: #6b7280;
}

.notice {
    padding: 12px 14px;
    background: #eff6ff;
    color: #1d4ed8;
    border-radius: 10px;
    margin-bottom: 16px;
}

.demand-group {
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    background: #fff;
    margin-bottom: 14px;
    overflow: hidden;
}

.demand-group:last-child {
    margin-bottom: 0;
}

.demand-group summary {
    list-style: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 14px 16px;
    background: #f8fafc;
    border-bottom: 1px solid #e5e7eb;
}

.demand-group summary::-webkit-details-marker {
    display: none;
}

.demand-group-title {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.demand-group-arrow {
    width: 10px;
    height: 10px;
    border-right: 2px solid #6b7280;
    border-bottom: 2px solid #6b7280;
    transform: rotate(45deg);
    transition: transform 0.16s ease;
    flex: 0 0 auto;
    margin-top: -2px;
}

.demand-group[open] .demand-group-arrow {
    transform: rotate(45deg);
}

.demand-group:not([open]) .demand-group-arrow {
    transform: rotate(-45deg);
}

.demand-group-meta {
    display: flex;
    align-items: center;
    gap: 14px;
    color: #6b7280;
    font-size: 13px;
    flex-wrap: wrap;
}

.create-shell {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 380px;
    gap: 18px;
}

.product-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 16px;
}

.product-card {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.05);
}

.product-thumb {
    height: 180px;
    background: linear-gradient(135deg, #f8fafc, #e5e7eb);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    cursor: zoom-in;
}

.product-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.product-body {
    padding: 14px;
}

.product-meta {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 10px;
}

.chip-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.chip {
    display: inline-flex;
    padding: 4px 8px;
    border-radius: 999px;
    background: #f3f4f6;
    color: #374151;
    font-size: 12px;
}

.upload-status-row {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.mini-btn {
    width: auto;
    padding: 6px 10px;
    font-size: 12px;
    border-radius: 999px;
}

.hidden {
    display: none !important;
}

.drawer-panel {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 16px;
    padding: 16px;
    box-shadow: 0 10px 24px rgba(15, 23, 42, 0.06);
    position: sticky;
    top: 20px;
    max-height: calc(100vh - 110px);
    overflow: auto;
}

.candidate-card {
    border: 1px solid #e5e7eb;
    border-radius: 14px;
    padding: 12px;
    margin-bottom: 12px;
    background: #fff;
}

.candidate-head {
    display: grid;
    grid-template-columns: 96px minmax(0, 1fr);
    gap: 12px;
    margin-bottom: 10px;
}

.candidate-thumb {
    width: 96px;
    height: 96px;
    border-radius: 12px;
    overflow: hidden;
    background: #f3f4f6;
}

.candidate-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.candidate-actions {
    display: flex;
    gap: 8px;
    margin-top: 10px;
}

.candidate-actions .btn,
.candidate-actions .btn-secondary,
.inline-actions .btn,
.inline-actions .btn-secondary {
    width: auto;
    padding: 8px 12px;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 24px;
    z-index: 1000;
}

.modal-backdrop.open {
    display: flex;
}

.modal-dialog {
    position: relative;
    width: min(1180px, 100%);
    max-height: calc(100vh - 48px);
    background: #f8fafc;
    border-radius: 20px;
    overflow: hidden;
    box-shadow: 0 30px 80px rgba(15, 23, 42, 0.28);
    display: grid;
    grid-template-columns: minmax(0, 1.1fr) 420px;
}

.modal-main,
.modal-side {
    overflow: auto;
}

.modal-main {
    background: #fff;
    padding: 20px;
}

.modal-side {
    border-left: 1px solid #e5e7eb;
    padding: 18px;
    background: #fbfbfc;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}

.close-btn {
    width: auto;
    padding: 8px 12px;
    background: #111827;
    color: #fff;
}

.modal-shell-close {
    position: absolute;
    top: 14px;
    right: 14px;
    z-index: 8;
    width: 36px;
    height: 36px;
    min-width: 36px;
    padding: 0;
    border-radius: 999px;
    border: 1px solid #d1d5db;
    background: rgba(255, 255, 255, 0.96);
    color: #111827;
    font-size: 13px;
    font-weight: 700;
    box-shadow: 0 8px 20px rgba(15, 23, 42, 0.12);
}

.modal-shell-close:hover {
    background: #fff;
}

.dropzone {
    border: 1px dashed #cbd5e1;
    border-radius: 14px;
    padding: 14px;
    background: #f8fafc;
}

.formula-panel-emphasis {
    background: linear-gradient(180deg, #fffdf4 0%, #ffffff 100%);
    border: 1px solid #f5d487;
    box-shadow: 0 10px 24px rgba(217, 119, 6, 0.08);
}

.formula-panel-emphasis .panel-header {
    padding-bottom: 10px;
    border-bottom: 1px dashed #f3d08a;
}

.formula-row {
    display: grid;
    grid-template-columns: minmax(0, 1.6fr) 100px auto;
    gap: 10px;
    margin-bottom: 10px;
    position: relative;
}

.formula-header {
    display: grid;
    grid-template-columns: minmax(0, 1.6fr) 100px auto;
    gap: 10px;
    margin-bottom: 10px;
    color: #6b7280;
    font-size: 13px;
    font-weight: 600;
}

.formula-field {
    position: relative;
}

.material-suggestions {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    right: 0;
    z-index: 30;
    background: #fff;
    border: 1px solid #d1d5db;
    border-radius: 12px;
    box-shadow: 0 16px 32px rgba(15, 23, 42, 0.12);
    max-height: 220px;
    overflow: auto;
}

.material-suggestion {
    padding: 10px 12px;
    border-bottom: 1px solid #f1f5f9;
    cursor: pointer;
}

.material-suggestion:last-child {
    border-bottom: 0;
}

.material-suggestion:hover {
    background: #f8fafc;
}

.material-suggestion.active {
    background: #e5e7eb;
}

.icon-btn {
    width: auto;
    min-width: 72px;
    height: 36px;
    padding: 0 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #d1d5db;
    background: #fff;
    color: #6b7280;
    gap: 6px;
    white-space: nowrap;
    flex-wrap: nowrap;
}

.icon-btn:hover {
    background: #f8fafc;
    color: #111827;
}

.suggestion-portal {
    position: fixed;
    z-index: 1200;
    background: #fff;
    border: 1px solid #d1d5db;
    border-radius: 12px;
    box-shadow: 0 16px 32px rgba(15, 23, 42, 0.14);
    max-height: 260px;
    overflow: auto;
}

.suggestion-portal.hidden {
    display: none;
}

.image-preview-float {
    position: fixed;
    z-index: 1400;
    background: #fff;
    border: 1px solid #d1d5db;
    border-radius: 16px;
    box-shadow: 0 24px 48px rgba(15, 23, 42, 0.18);
    overflow: hidden;
    min-width: 320px;
    min-height: 260px;
}

.image-preview-float-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 14px;
    border-bottom: 1px solid #e5e7eb;
    background: #f8fafc;
    cursor: move;
    user-select: none;
}

.mini-close-btn {
    width: 32px;
    height: 32px;
    min-width: 32px;
    padding: 0;
    border-radius: 999px;
    background: #fff;
    color: #111827;
    border: 1px solid #d1d5db;
    font-size: 12px;
}

.image-preview-float-body {
    height: calc(100% - 53px);
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f3f4f6;
    cursor: grab;
    user-select: none;
    touch-action: none;
    overflow: hidden;
    isolation: isolate;
}

.image-preview-float-body.is-panning {
    cursor: grabbing;
}

.image-preview-float-body img {
    position: relative;
    z-index: 1;
    width: 100%;
    height: 100%;
    object-fit: contain;
    transform-origin: center center;
    transition: transform 0.04s linear;
    will-change: transform;
    pointer-events: none;
    user-select: none;
}

.empty-state {
    padding: 30px 18px;
    border: 1px dashed #d1d5db;
    border-radius: 16px;
    color: #6b7280;
    text-align: center;
    background: #fff;
}

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

.inline-actions > button,
.inline-actions > .btn,
.inline-actions > .btn-secondary {
    width: auto;
    white-space: nowrap;
    flex: 0 0 auto;
}

@media (max-width: 960px) {
    .app-shell,
    .split,
    .grid-2,
    .grid-3,
    .grid-4 {
        grid-template-columns: 1fr;
        display: block;
    }

    .sidebar {
        display: none;
    }

    .create-shell,
    .modal-dialog,
    .formula-row {
        grid-template-columns: 1fr;
    }

    .candidate-head {
        grid-template-columns: 1fr;
    }
}
