/* ============================================================
   血糖管理器 - 样式表
   响应式设计：PC 宽表格；移动端横向滚动 + 首列冻结
   ============================================================ */

:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --bg: #f1f5f9;
    --card: #ffffff;
    --text: #1e293b;
    --text-soft: #64748b;
    --border: #e2e8f0;
    --normal: #16a34a;   /* 正常-绿 */
    --high: #dc2626;     /* 偏高-红 */
    --low: #f59e0b;      /* 偏低-橙 */
    --normal-bg: #f0fdf4;
    --high-bg: #fef2f2;
    --low-bg: #fffbeb;
    --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
    --radius: 12px;
}

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

html, body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-tap-highlight-color: transparent;
    font-size: 15px;
}

button { font-family: inherit; cursor: pointer; border: none; background: none; }
input, textarea { font-family: inherit; font-size: 15px; }

/* ============ 顶部栏 ============ */
.topbar {
    position: sticky; top: 0; z-index: 50;
    display: flex; align-items: center; justify-content: space-between;
    background: var(--card);
    padding: 10px 16px;
    box-shadow: var(--shadow);
    gap: 12px;
}
.brand { font-weight: 700; font-size: 18px; white-space: nowrap; }
.topbar-right { display: flex; align-items: center; gap: 10px; }

.view-switch { display: flex; background: var(--bg); border-radius: 10px; padding: 3px; }
.view-btn {
    padding: 7px 14px; border-radius: 8px; font-size: 14px; color: var(--text-soft);
    font-weight: 600; transition: .15s;
}
.view-btn.active { background: var(--primary); color: #fff; box-shadow: var(--shadow); }

.user-chip {
    display: flex; align-items: center; gap: 6px;
    background: var(--bg); padding: 7px 12px; border-radius: 20px;
    font-weight: 600; font-size: 14px; color: var(--text);
}
.user-chip:hover { background: #e2e8f0; }
.user-avatar { font-size: 15px; }

/* ============ 工具栏 ============ */
.toolbar {
    display: flex; align-items: center; justify-content: space-between;
    flex-wrap: wrap; gap: 10px;
    padding: 12px 16px;
}
.range-group { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.range-group label { font-size: 13px; color: var(--text-soft); display: flex; align-items: center; gap: 4px; }
.range-group input[type=date] {
    border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; background: #fff;
}
.quick-range { display: flex; gap: 6px; }
.chip-btn {
    padding: 6px 12px; border-radius: 20px; font-size: 13px; background: #fff;
    border: 1px solid var(--border); color: var(--text-soft); font-weight: 600;
}
.chip-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.toolbar-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.ref-switch, .unit-switch { display: flex; gap: 6px; }

.primary-btn {
    background: var(--primary); color: #fff; padding: 9px 16px; border-radius: 10px;
    font-weight: 600; font-size: 14px; transition: .15s;
}
.primary-btn:hover { background: var(--primary-dark); }
.ghost-btn { padding: 9px 16px; border-radius: 10px; color: var(--text-soft); font-weight: 600; }
.ghost-btn:hover { background: var(--bg); }
.danger-btn { padding: 9px 16px; border-radius: 10px; color: var(--high); font-weight: 600; }
.danger-btn:hover { background: var(--high-bg); }

/* ============ 视图容器 ============ */
.view { padding: 0 16px 40px; }

/* ============ 统计看板 ============ */
.stats-panel {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 14px 16px;
    margin-bottom: 14px;
}
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 10px;
}
.stat-card {
    background: var(--bg);
    border-radius: 10px;
    padding: 12px 14px;
}
.stat-card.highlight { background: #dbeafe; }
.stat-label { font-size: 12px; color: var(--text-soft); font-weight: 600; margin-bottom: 4px; }
.stat-value { font-size: 22px; font-weight: 800; color: var(--text); line-height: 1.2; }
.stat-value.sm { font-size: 17px; }
.stat-value small { font-size: 12px; font-weight: 600; color: var(--text-soft); margin-left: 4px; }
.stat-sub { font-size: 11px; color: var(--text-soft); margin-top: 4px; }
.stat-empty { color: var(--text-soft); font-size: 13px; padding: 8px 0; }

.food-links { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 12px; }
.food-links-title { font-size: 13px; font-weight: 700; color: var(--text); margin-bottom: 8px; }
.food-links-list { display: flex; flex-direction: column; gap: 6px; max-height: 180px; overflow-y: auto; }
.food-link-item {
    display: grid;
    grid-template-columns: 48px 48px 1fr auto;
    gap: 8px; align-items: center;
    padding: 8px 10px; border-radius: 8px; background: var(--bg);
    font-size: 13px;
}
.food-link-item.fl-high { background: var(--high-bg); }
.food-link-item.fl-low { background: var(--low-bg); }
.fl-date { font-weight: 700; color: var(--text-soft); }
.fl-meal { color: #d97706; font-weight: 600; }
.fl-food { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fl-val { font-weight: 800; white-space: nowrap; }
.fl-high .fl-val { color: var(--high); }
.fl-low .fl-val { color: var(--low); }
.fl-ok .fl-val { color: var(--normal); }

/* ============ 表格 ============ */
.table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    background: var(--card);
}
#glucose-table {
    border-collapse: separate; border-spacing: 0;
    width: 100%;
    table-layout: fixed; /* 按百分比分配列宽 */
}
#glucose-table th, #glucose-table td {
    border-bottom: 1px solid var(--border);
    border-right: 1px solid var(--border);
    padding: 0; text-align: center;
    min-width: 0; /* 允许按比例收缩 */
}
/* 日期 + 8 个血糖时段 ≈ 整体 60%（5% + 8×6.875%） */
#glucose-table td.reading-cell,
#glucose-table thead th:not(.col-date):not(.col-meal):not(.col-med) {
    width: 6.875%;
}
#glucose-table thead th {
    background: #f8fafc; position: sticky; top: 0; z-index: 10;
    padding: 10px 6px; font-size: 13px; font-weight: 700; color: var(--text-soft);
    white-space: nowrap;
}
.th-group { font-size: 11px; color: var(--primary); font-weight: 700; }

/* 首列（日期）冻结：必须用更高优先级盖住横向滑过的其他表头 */
.col-date,
#glucose-table td.col-date,
#glucose-table th.col-date {
    position: sticky; left: 0; z-index: 20;
    background: #ffffff;
    width: 5%;
    box-shadow: 3px 0 6px rgba(15, 23, 42, 0.1);
}
#glucose-table thead th.col-date {
    position: sticky;
    top: 0;
    left: 0;
    z-index: 40; /* 高于其他 thead th 的 z-index:10 */
    background: #f8fafc;
    box-shadow: 3px 0 6px rgba(15, 23, 42, 0.12);
}
.date-cell { padding: 8px 8px; white-space: nowrap; }
.date-main { font-weight: 700; font-size: 14px; white-space: nowrap; }
.date-week { font-size: 11px; color: var(--text-soft); white-space: nowrap; }
.date-today { color: var(--primary); }

/* 血糖数据格子 */
.reading-cell { cursor: pointer; transition: background .12s; height: 78px; padding: 10px 4px; vertical-align: middle; }
.reading-cell:hover { background: #f8fafc; }
.reading-cell .val-row {
    display: flex; align-items: baseline; justify-content: center; gap: 14px;
    flex-wrap: nowrap;
}
.reading-cell .val { font-size: 19px; font-weight: 800; line-height: 1.1; }
.reading-cell .meta {
    font-size: 13px; color: #334155; line-height: 1.1; font-weight: 700;
    white-space: nowrap;
}
.reading-cell .note {
    font-size: 12.5px; color: #475569; font-weight: 600;
    max-width: 90%; margin: 10px auto 0; overflow: hidden;
    text-overflow: ellipsis; white-space: nowrap;
}
.reading-cell.empty { color: #cbd5e1; }
.reading-cell.empty::after { content: "＋"; font-size: 16px; font-weight: 400; }

/* 数值颜色状态 */
.v-normal .val { color: var(--normal); }
.v-high   { background: var(--high-bg); }
.v-high .val { color: var(--high); }
.v-low    { background: var(--low-bg); }
.v-low .val { color: var(--low); }

/* 食物列 */
/* 食物列：三餐固定等宽，内容再多也不撑开格子 */
/* 三餐 + 用药 ≈ 整体 40%（11%×3 + 7%） */
.col-meal,
#glucose-table th.col-meal,
#glucose-table td.meal-cell {
    width: 11%;
}
.meal-cell {
    cursor: pointer; padding: 8px;
    font-size: 12.5px; text-align: left;
    color: var(--text); line-height: 1.4; vertical-align: middle;
    white-space: normal;
    word-break: break-word;
    overflow-wrap: anywhere;
}
.meal-cell:hover { background: #f8fafc; }
.meal-cell.empty {
    color: #cbd5e1; text-align: center;
    white-space: nowrap; word-break: normal;
}
.meal-cell.empty::after { content: "＋ 记录食物"; font-size: 12px; }
.th-meal { color: #d97706; }

/* 用药列 */
.col-med,
#glucose-table th.col-med,
#glucose-table td.med-cell {
    width: 7%;
}
.th-med { color: #7c3aed; }
.med-cell {
    cursor: pointer; padding: 8px;
    font-size: 12.5px; text-align: left;
    color: var(--text); line-height: 1.4; vertical-align: middle;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.med-cell:hover { background: #f5f3ff; }
.med-cell.empty { color: #cbd5e1; text-align: center; }
.med-cell.empty::after { content: "＋ 用药"; font-size: 12px; }

/* ============ 图例 ============ */
.legend { display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
    margin-top: 14px; font-size: 13px; color: var(--text-soft); }
.legend .dot { display: inline-block; width: 11px; height: 11px; border-radius: 50%; margin-right: 5px; vertical-align: -1px; }
.dot-normal { background: var(--normal); }
.dot-high { background: var(--high); }
.dot-low { background: var(--low); }
.legend-note { font-size: 12px; }

/* ============ 图表 ============ */
.chart-mode-bar {
    display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
    margin: 4px 0 10px;
}
.chart-mode-label { font-size: 13px; color: var(--text-soft); font-weight: 600; }
.chart-mode-switch { display: flex; gap: 6px; }
.chart-opt {
    display: flex; align-items: center; gap: 5px;
    font-size: 13px; color: var(--text-soft); font-weight: 600;
    cursor: pointer; user-select: none;
}
.chart-opt input { accent-color: var(--primary); width: 15px; height: 15px; }
.chart-controls { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 16px; }
.slot-toggle {
    padding: 6px 12px; border-radius: 20px; font-size: 13px; font-weight: 600;
    border: 1.5px solid var(--border); background: #fff; color: var(--text-soft);
}
.slot-toggle.active { color: #fff; }
.chart-wrap { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
    padding: 16px; height: 60vh; min-height: 340px; }

/* ============ 弹窗通用 ============ */
.overlay {
    position: fixed; inset: 0; z-index: 100;
    background: rgba(15,23,42,.5);
    display: flex; align-items: center; justify-content: center;
    padding: 16px; backdrop-filter: blur(2px);
}
/* display:flex 会覆盖浏览器默认的 [hidden]{display:none}，必须显式处理 */
.overlay[hidden] {
    display: none !important;
}
.modal {
    background: var(--card); border-radius: 16px; width: 100%;
    max-width: min(400px, calc(100vw - 32px));
    padding: 22px; box-shadow: 0 20px 50px rgba(0,0,0,.25);
    animation: pop .18s ease;
    overflow-x: hidden;
    box-sizing: border-box;
}
.modal-wide { max-width: 480px; }
@keyframes pop { from { transform: scale(.94); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.modal-title { font-size: 18px; font-weight: 700; }
.modal-sub { font-size: 13px; color: var(--text-soft); margin: 4px 0 16px; }
.meal-tip {
    background: #fff7ed; border: 1px solid #fed7aa; color: #9a3412;
    border-radius: 10px; padding: 10px 12px; font-size: 13px; line-height: 1.45;
    margin-bottom: 14px;
}
.meal-tip em { font-style: normal; color: var(--text-soft); }
.field { display: block; margin-bottom: 14px; width: 100%; max-width: 100%; box-sizing: border-box; }
.field span { display: block; font-size: 13px; color: var(--text-soft); margin-bottom: 6px; font-weight: 600; }
.field input, .field textarea {
    width: 100%; max-width: 100%; min-width: 0;
    border: 1.5px solid var(--border); border-radius: 10px; padding: 10px 12px;
    outline: none; transition: border .15s; resize: vertical;
    box-sizing: border-box;
}
/* 部分浏览器 time 控件默认很宽，强制限制在弹窗内 */
.field input[type="time"] {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    display: block;
    -webkit-appearance: none;
    appearance: none;
}
.field input[type="time"]::-webkit-calendar-picker-indicator {
    margin-left: 0;
}
.field input:focus, .field textarea:focus { border-color: var(--primary); }
.field.grow { flex: 1; min-width: 0; }
.modal-actions { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
.modal-actions .spacer { flex: 1; }

/* 用药弹窗 */
.med-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; max-height: 28vh; overflow-y: auto; }
.med-empty { font-size: 13px; color: var(--text-soft); padding: 8px 0; }
.med-item {
    display: flex; align-items: center; gap: 10px;
    background: var(--bg); border-radius: 10px; padding: 10px 12px;
}
.med-item-main { flex: 1; text-align: left; font-size: 14px; display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: baseline; }
.med-dose { color: var(--primary); font-weight: 700; }
.med-time { color: var(--text-soft); font-size: 12px; font-weight: 600; }
.med-note { color: var(--text-soft); font-size: 12px; }
.med-form {
    border: 1px dashed var(--border); border-radius: 12px; padding: 12px;
    margin-bottom: 8px;
}
.med-form-title { font-size: 13px; font-weight: 700; margin-bottom: 10px; color: var(--text-soft); }
.med-form-row { display: flex; gap: 10px; flex-wrap: wrap; }
.med-form .primary-btn { width: 100%; margin-top: 4px; }

/* ============ 登录卡片 ============ */
.login-card {
    background: var(--card); border-radius: 20px; padding: 34px 28px; width: 100%; max-width: 380px;
    text-align: center; box-shadow: 0 20px 60px rgba(0,0,0,.2);
}
.login-logo { font-size: 46px; }
.login-card h1 { font-size: 24px; margin: 8px 0 4px; }
.login-sub { font-size: 13px; color: var(--text-soft); margin-bottom: 22px; }
.login-card input {
    width: 100%; border: 1.5px solid var(--border); border-radius: 12px; padding: 13px 14px;
    font-size: 16px; text-align: center; outline: none; margin-bottom: 14px;
}
.login-card input:focus { border-color: var(--primary); }
#login-btn {
    width: 100%; background: var(--primary); color: #fff; padding: 13px; border-radius: 12px;
    font-size: 16px; font-weight: 700;
}
#login-btn:hover { background: var(--primary-dark); }
.login-history { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 16px; }
.login-history .hist-btn {
    padding: 6px 12px; border-radius: 20px; background: var(--bg); font-size: 13px;
    color: var(--text-soft); font-weight: 600;
}
.login-history .hist-btn:hover { background: #e2e8f0; }

/* ============ 用户列表 ============ */
.user-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; max-height: 40vh; overflow-y: auto; }
.user-row {
    display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 10px;
    background: var(--bg); font-weight: 600;
}
.user-row .u-name { flex: 1; text-align: left; cursor: pointer; }
.user-row.active { background: #dbeafe; color: var(--primary-dark); }
.user-row .u-del { color: var(--text-soft); font-size: 13px; padding: 4px 8px; border-radius: 6px; }
.user-row .u-del:hover { color: var(--high); background: var(--high-bg); }

/* ============ Toast ============ */
.toast {
    position: fixed; bottom: 30px; left: 50%; transform: translateX(-50%);
    background: #1e293b; color: #fff; padding: 11px 20px; border-radius: 10px;
    font-size: 14px; z-index: 200; box-shadow: var(--shadow);
    animation: toastin .2s ease;
}
@keyframes toastin { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

.empty-hint { text-align: center; color: var(--text-soft); padding: 40px 20px; font-size: 14px; }

/* ============ 移动端适配 ============ */
@media (max-width: 640px) {
    html, body { font-size: 12px; }
    .brand { font-size: 14px; }
    .brand-full { display: none; }
    .topbar { padding: 5px 8px; }
    .view-btn { padding: 4px 7px; font-size: 11px; }
    .user-chip { padding: 4px 7px; font-size: 11px; }
    .toolbar { padding: 6px 8px; gap: 6px; }
    .chip-btn { padding: 3px 7px; font-size: 11px; }
    .primary-btn { padding: 6px 10px; font-size: 12px; }
    .view { padding: 0 6px 24px; }

    /* 统计区更紧凑 */
    .stats-panel { padding: 8px; margin-bottom: 8px; }
    .stat-card { padding: 6px 8px; }
    .stat-label { font-size: 10px; margin-bottom: 2px; }
    .stat-value { font-size: 15px; }
    .stat-value.sm { font-size: 12px; }
    .stat-sub { font-size: 10px; margin-top: 2px; }
    .stats-grid { grid-template-columns: repeat(3, 1fr); gap: 5px; }
    .food-links { margin-top: 8px; padding-top: 6px; }
    .food-links-title { font-size: 11px; margin-bottom: 6px; }
    .food-link-item { grid-template-columns: 36px 36px 1fr auto; font-size: 10px; padding: 4px 6px; }

    /* 表头 / 日期列压缩 */
    #glucose-table { min-width: 580px; }
    #glucose-table thead th { padding: 4px 1px; font-size: 10px; }
    #glucose-table th, #glucose-table td { min-width: 48px; }
    #glucose-table th.col-date,
    #glucose-table td.col-date {
        min-width: 50px;
        max-width: 50px;
        width: 50px;
        padding-left: 4px;
        padding-right: 4px;
    }
    .date-cell { padding: 2px 4px; white-space: nowrap; }
    .date-main { font-size: 10px; white-space: nowrap; }
    .date-week { font-size: 8px; white-space: nowrap; }

    /* 血糖格子：尽量一屏多看 */
    .reading-cell {
        height: 42px;
        padding: 1px 0;
    }
    .reading-cell .val-row {
        flex-direction: column;
        align-items: center;
        gap: 0;
    }
    .reading-cell .val { font-size: 12px; }
    .reading-cell .meta { font-size: 8px; font-weight: 600; }
    .reading-cell .note {
        font-size: 7px;
        max-width: 42px;
        margin-top: 0;
    }
    .reading-cell.empty::after { font-size: 11px; }

    .col-meal,
    #glucose-table th.col-meal,
    #glucose-table td.meal-cell {
        width: 100px;
        min-width: 100px;
        max-width: 100px;
    }
    .meal-cell {
        padding: 2px 4px; font-size: 10px;
        white-space: normal;
        word-break: break-word;
        overflow-wrap: anywhere;
        line-height: 1.35;
    }
    .meal-cell.empty { white-space: nowrap; }
    .meal-cell.empty::after { font-size: 10px; content: "＋"; }

    .col-med,
    #glucose-table th.col-med,
    #glucose-table td.med-cell {
        width: 68px;
        min-width: 68px;
        max-width: 68px;
    }
    .med-cell { padding: 2px 3px; font-size: 10px; }
    .med-cell.empty::after { font-size: 10px; content: "＋"; }

    .legend { margin-top: 8px; font-size: 11px; gap: 8px; }
    .legend-note { font-size: 10px; }
    .modal { padding: 16px; }
}
