* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-primary: #0a0e27;
    --bg-secondary: #141937;
    --bg-card: #1a1f3a;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --border: #2d3548;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    overflow-x: hidden;
}

.header {
    background: linear-gradient(135deg, var(--bg-secondary) 0%, var(--bg-card) 100%);
    padding: 1.25rem 2rem 1.5rem;
    position: relative;
    border-bottom: 1px solid var(--border);
}

.header::after {
    display: none;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem 2rem 0;
}

h1 {
    font-size: 1.75rem;
    font-weight: 700;
    margin-bottom: 0.25rem;
    background: linear-gradient(135deg, #60a5fa 0%, #3b82f6 50%, #2563eb 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 1.1rem;
}

.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 2rem;
    margin-top: 1rem;
    font-size: 0.9rem;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--success);
    animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 1.5rem;
    margin: 0 0 1.5rem;
    position: relative;
    z-index: 1;
}

.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 1rem;
    padding: 1.5rem;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
}

.stat-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--accent), var(--success));
    transform: translateX(-100%);
    transition: transform 0.3s ease;
}

.stat-card:hover::before {
    transform: translateX(0);
}

.stat-card:hover {
    transform: translateY(-4px);
    border-color: var(--accent);
    box-shadow: 0 20px 40px rgba(59, 130, 246, 0.1);
}

.stat-label {
    color: var(--text-secondary);
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 0.5rem;
    font-weight: 600;
}

.stat-value {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 0.25rem;
    background: linear-gradient(135deg, var(--text-primary) 0%, var(--text-secondary) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.stat-change {
    font-size: 0.85rem;
    color: var(--success);
}

.chart-container {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 1rem;
    padding: 1.5rem;
    margin-bottom: 2rem;
    position: relative;
}

.chart-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}

.chart-title {
    font-size: 1.25rem;
    font-weight: 600;
}

.chart-time {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

canvas {
    max-height: 300px;
}

.recent-items {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 1rem;
    padding: 1.5rem;
    margin-bottom: 2rem;
}

.recent-header {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 1.5rem;
}

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

.recent-table {
    width: 100%;
    border-collapse: collapse;
    min-width: 720px;
}

.recent-table th,
.recent-table td {
    text-align: left;
    padding: 0.85rem 0.75rem;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}

.recent-table th {
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    font-size: 0.78rem;
    font-weight: 600;
}

.recent-table tbody tr {
    transition: background 0.2s ease;
}

.recent-table tbody tr:hover {
    background: rgba(59, 130, 246, 0.08);
}

.recent-table tbody tr.history-row {
    cursor: pointer;
}

.recent-table tbody tr.history-row.is-selected {
    background: rgba(16, 185, 129, 0.14);
}

.col-name {
    font-weight: 600;
    color: var(--text-primary);
}

.col-price {
    color: var(--success);
    font-weight: 700;
    white-space: nowrap;
}

.movement-up {
    color: var(--success);
    font-weight: 600;
    white-space: nowrap;
}

.movement-down {
    color: var(--danger);
    font-weight: 600;
    white-space: nowrap;
}

.movement-flat {
    color: var(--text-secondary);
    font-weight: 600;
    white-space: nowrap;
}

.col-tags {
    min-width: 210px;
}

.buy-order-log-meta {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-bottom: 1rem;
}

.buy-log-table {
    min-width: 1100px;
}

.log-pill {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 700;
    padding: 0.2rem 0.55rem;
    letter-spacing: 0.02em;
}

.log-pill-ok {
    color: #d1fae5;
    background: rgba(16, 185, 129, 0.18);
    border: 1px solid rgba(16, 185, 129, 0.28);
}

.log-pill-reject {
    color: #fecaca;
    background: rgba(239, 68, 68, 0.16);
    border: 1px solid rgba(239, 68, 68, 0.26);
}

.tag-empty {
    color: var(--text-secondary);
}

.empty-state {
    color: var(--text-secondary);
    text-align: center;
    padding: 1.5rem;
}

.item-row {
    display: flex;
    align-items: center;
    padding: 1rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    margin-bottom: 0.75rem;
    transition: all 0.2s ease;
}

.item-row:hover {
    background: var(--bg-primary);
    border-color: var(--accent);
    transform: translateX(4px);
}

.item-image {
    width: 48px;
    height: 48px;
    border-radius: 0.5rem;
    margin-right: 1rem;
    object-fit: contain;
    background: var(--bg-primary);
    padding: 4px;
}

.item-info {
    flex: 1;
}

.item-name {
    font-weight: 500;
    margin-bottom: 0.25rem;
}

.item-meta {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.item-price {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--success);
    white-space: nowrap;
}

.badge {
    display: inline-block;
    padding: 0.25rem 0.5rem;
    border-radius: 0.25rem;
    font-size: 0.75rem;
    font-weight: 600;
    margin-left: 0.5rem;
}

.badge-verified {
    background: rgba(16, 185, 129, 0.2);
    color: var(--success);
}

.badge-suspicious {
    background: rgba(239, 68, 68, 0.2);
    color: var(--danger);
}

.badge-accurate {
    background: rgba(59, 130, 246, 0.2);
    color: var(--accent);
}

.loading {
    text-align: center;
    padding: 3rem;
    color: var(--text-secondary);
}

.spinner {
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    animation: spin 1s linear infinite;
    margin: 0 auto 1rem;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

footer {
    text-align: center;
    padding: 2rem;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

@media (max-width: 768px) {
    .stats-grid {
        grid-template-columns: 1fr;
    }

    h1 {
        font-size: 2rem;
    }

    .header {
        padding: 1.5rem 1rem 3rem;
    }

    .container {
        padding: 0 1rem;
    }
}

/* ── Item link in table ──────────────────────────────────────────────────── */
.item-link {
    color: var(--text-primary);
    text-decoration: none;
    font-weight: 600;
}
.item-link:hover {
    color: var(--accent);
    text-decoration: underline;
}

/* ── Search section ──────────────────────────────────────────────────────── */
.search-section {
    margin: 0 0 1.5rem;
    position: relative;
    z-index: 10;
}
.search-box {
    position: relative;
    width: 100%;
}
.search-input-wrap {
    display: flex;
    align-items: center;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    padding: 0 1rem;
    transition: border-color 0.2s, box-shadow 0.2s;
}
.search-input-wrap:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}
.search-icon {
    font-size: 1rem;
    color: var(--text-secondary);
    margin-right: 0.5rem;
    pointer-events: none;
    user-select: none;
}
.search-input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-primary);
    font-size: 0.95rem;
    padding: 0.85rem 0;
}
.search-input::placeholder {
    color: var(--text-secondary);
}
.search-clear {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 1rem;
    padding: 0.25rem 0.4rem;
    border-radius: 0.25rem;
    line-height: 1;
    transition: color 0.15s, background 0.15s;
}
.search-clear:hover {
    color: var(--text-primary);
    background: var(--bg-secondary);
}
.search-results-dropdown {
    display: none;
    position: absolute;
    top: calc(100% + 0.35rem);
    left: 0;
    right: 0;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.45);
    overflow: hidden;
    z-index: 100;
    max-height: 420px;
    overflow-y: auto;
}
.search-result-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.7rem 1rem;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    transition: background 0.15s;
}
.search-result-row:last-child { border-bottom: none; }
.search-result-row:hover,
.search-result-row:focus {
    background: rgba(59, 130, 246, 0.1);
    outline: none;
}
.search-result-img {
    width: 36px;
    height: 36px;
    object-fit: contain;
    border-radius: 0.25rem;
    background: var(--bg-secondary);
    flex-shrink: 0;
}
.search-result-img-placeholder {
    width: 36px;
    height: 36px;
    border-radius: 0.25rem;
    background: var(--bg-secondary);
    flex-shrink: 0;
}
.search-result-info { flex: 1; min-width: 0; }
.search-result-name {
    font-weight: 600;
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text-primary);
}
.search-result-price {
    font-size: 0.8rem;
    color: var(--success);
    margin-top: 0.1rem;
}
.search-no-results {
    padding: 1rem;
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

/* ── Back link (item page) ───────────────────────────────────────────────── */
.back-link {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 0.9rem;
    margin-bottom: 1rem;
    padding: 0.35rem 0.75rem;
    border-radius: 0.5rem;
    border: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.04);
    transition: color 0.2s, border-color 0.2s, background 0.2s;
}
.back-link:hover {
    color: var(--text-primary);
    border-color: var(--accent);
    background: rgba(59, 130, 246, 0.08);
}

/* ── Item page price grid ────────────────────────────────────────────────── */
.item-price-grid {
    margin-top: 2rem !important;
    grid-template-columns: repeat(5, minmax(0, 1fr));
}

.item-price-grid .stat-card {
    padding: 1rem 1.25rem;
}

.item-price-grid .stat-value {
    font-size: 1.75rem;
}

@media (max-width: 1600px) {
    .item-price-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }
}

@media (max-width: 1100px) {
    .item-price-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 768px) {
    .item-price-grid {
        grid-template-columns: 1fr;
    }
}

/* ── Item page action row ───────────────────────────────────────────────── */
.item-page-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.refresh-item-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-family: inherit;
    border: 1px solid var(--accent);
    background: rgba(59, 130, 246, 0.15);
    color: var(--accent);
    border-radius: 0.5rem;
    padding: 0.38rem 0.75rem;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s, color 0.2s, opacity 0.2s;
}

.refresh-item-btn:hover {
    background: rgba(59, 130, 246, 0.25);
    border-color: #60a5fa;
    color: #dbeafe;
}

.refresh-item-btn:disabled {
    cursor: not-allowed;
    opacity: 0.65;
}

.refresh-item-status {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.refresh-item-status.error {
    color: #f87171;
}

/* ── Item page badges row ────────────────────────────────────────────────── */
.item-page-badges {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.75rem;
}
.item-page-badges .badge { margin-left: 0; }

/* ── Extra badge types ───────────────────────────────────────────────────── */
.badge-stn {
    background: rgba(245, 158, 11, 0.2);
    color: var(--warning);
}
.item-type-label {
    display: inline-block;
    padding: 0.25rem 0.6rem;
    border-radius: 0.25rem;
    font-size: 0.75rem;
    font-weight: 600;
    background: rgba(148, 163, 184, 0.12);
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

/* ── Not found state ─────────────────────────────────────────────────────── */
.not-found-state {
    text-align: center;
    padding: 5rem 2rem;
    color: var(--text-secondary);
    font-size: 1.1rem;
}
.not-found-state a {
    display: inline-block;
    margin-top: 1.5rem;
    color: var(--accent);
    text-decoration: none;
}
.not-found-state a:hover { text-decoration: underline; }
