* {
    box-sizing:border-box;
}

/* 屏幕阅读器可见、视觉上隐藏。用于 <h1> 和隐藏的文件输入框。 */
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
}

body {
    background: #FBDAB5;
    margin: 0;
    font-family: Georgia, 'Times New Roman', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    font-style: normal;
    font-weight: normal;
    line-height: normal;
    font-size: 1.2em;
    letter-spacing: 0.03em;
}

button { 
   font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
   font-weight: bolder;
}

/*************HEADER SECTION****************/
header {
    height: 40vh;
    background-color: rgb(252, 136, 4);
    /* 用 1920 宽的派生图而不是 images/xinsanguo-csm.jpg（2752x1457、920KB）：
       header 最多也就铺满视口宽度，原图那一圈分辨率纯属白下。
       原图留着做母版，派生图由它缩出来。 */
    background: url(images/xinsanguo-csm-1920.jpg) no-repeat;
    background-size: cover;
    /* 图是 1.89:1，header 只有 40vh，cover 会上下裁掉一部分。
       图中间那行对白落在纵向 44%~55%，center 在常见视口下可见区间约
       26%~74%（最矮的 1920x700 是 36%~64%），对白始终在可视区内。
       改 header 高度或换图时要重新核对这一点。 */
    background-position: center;
    position: relative;
}

/* 头图里自带「吃什么」的对白，页面不再往上叠文字，
   原来给那两个 <span> 写的定位/描边样式一并删掉。 */

/*************MAIN SECTION****************/
main {
    padding: 10px;
    position: relative;
}

.main-nav {
    display: flex;
    gap: 0.5rem;
    padding: 0.75rem 10px 0;
    background: #FBDAB5;
}

.nav-button {
    padding: 0.6rem 0.9rem;
    border: 1px solid #CE760C;
    border-radius: 6px 6px 0 0;
    background: #DFDFA9;
    color: #4f320c;
    cursor: pointer;
}

/* #6b6b33 上的白字对比度 5.56:1，原来的 #999955 只有 2.98:1。 */
.nav-button.active {
    background: #6b6b33;
    color: #fff;
}

button:disabled {
    cursor: wait;
    opacity: 0.65;
}

.status-bar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    max-width: 78rem;
    margin: 0.5rem auto;
}

.status-bar .app-status {
    flex: 1 1 auto;
    margin: 0;
}

.retry-button {
    flex: 0 0 auto;
    min-height: 2.25rem;
    padding: 0 1rem;
    cursor: pointer;
}

.app-status {
    min-height: 1.5rem;
    margin: 0.5rem auto;
    color: #4f320c;
}

.app-status.error,
.form-error {
    color: #9b1c1c;
}

.app-status.success {
    color: #2d6a32;
}

.app-view[hidden] {
    display: none;
}

.app-view h2 {
    margin-top: 0.5rem;
}

.home-choice-layout {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1rem;
    max-width: 78rem;
    margin: 2rem auto 0;
    align-items: stretch;
}

.random-choice-panel {
    max-width: none;
    margin: 0;
    padding: 2rem;
    border: 1px solid rgba(95, 56, 12, 0.2);
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.25);
    text-align: center;
}

.random-choice-panel > button {
    min-width: 8rem;
    padding: 0.7rem 1rem;
    cursor: pointer;
}

.search-choice-panel {
    padding: 2rem;
    border: 1px solid rgba(95, 56, 12, 0.2);
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.25);
}

.search-choice-panel h2 {
    margin-top: 0;
}

.food-search-input {
    display: block;
    width: min(100%, 28rem);
    margin-top: 0.75rem;
}

.search-note {
    margin-bottom: 0.75rem;
    color: #7a4b18;
    font-size: 0.9em;
}

.search-results {
    display: grid;
    gap: 0.4rem;
    margin: 0;
    padding: 0;
    list-style: none;
}

.search-results button {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 2.25rem;
    padding: 0.55rem 0.75rem;
    border: 1px solid #CE760C;
    border-radius: 5px;
    background: #DFDFA9;
    color: #4f320c;
    cursor: pointer;
    line-height: 1.2;
    text-align: center;
}

.search-results button:hover,
.search-results button:focus-visible {
    background: #6b6b33;
    color: #fff;
}

.empty-search {
    padding: 0.5rem 0;
    color: #7a4b18;
}

/* 关键词没有全部命中时的提示：结果本身仍然可用，所以不用错误色。 */
.search-partial-note {
    padding: 0.35rem 0;
    color: #7a4b18;
    font-size: 0.9em;
}

/* 搜索结果里补一行「别名 … · 标签 …」，让人看得出为什么这条命中。 */
.search-result-meta {
    margin-top: 0.15rem;
    font-size: 0.85em;
    opacity: 0.85;
}

.food-chooser {
    margin-top: 1.5rem;
    font-size: 1.35rem;
}

.food-chooser span {
    font-weight: bold;
}

.random-choice-panel #form-error {
    margin-bottom: 0.75rem;
}

.choice-input {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0.75rem 0;
}

.choice-input input {
    max-width: 28rem;
}

.hori {
    border: 0.5px solid #CE760C;
    margin: 15px 0;
}

.intro p {
    margin-top: 1em;
}

ul {
    margin-top: 0;
    margin-left: 20px;
    padding: 10px 0;
}

/* 候选项卡片墙：窄屏一列，宽屏自动铺成多列。
   以前是一列限宽 38rem，43 个候选项排下来就是一条很长的窄带；
   每项还固定摊着两个输入框，整页几乎全是空控件。 */
.foodlist {
    list-style-type: none;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr));
    gap: 0.6rem;
    align-items: start;
    max-width: 78rem;
    margin: 1rem auto;
    padding-left: 0;
}

.foodlist li {
    background: rgba(255, 255, 255, 0.35);
    border: 1px solid rgba(95, 56, 12, 0.25);
    border-radius: 8px;
    padding: 0.5rem 0.6rem;
    text-align: left;
}

/* 展开的那张卡占满一整行，别名和标签才排得下左右两栏。 */
.foodlist li.choice-expanded {
    grid-column: 1 / -1;
    background: #DFDFA9;
}

.foodlist .delete-choice {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    /* 至少 24x24 的触控目标（WCAG 2.5.8）。 */
    min-width: 24px;
    min-height: 24px;
    padding: 0 0.3rem;
    background: #C80C07;
    border-color: #C80C07;
    cursor: pointer;
}

/* 白字在 #C80C07 上对比度 5.98:1，原来的 #999955 只有 2.01:1。 */
.foodlist .delete-icon {
    color: #fff;
    font-size: 1.1rem;
    line-height: 1;
}

.choice-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.4rem;
}

.choice-name {
    font-weight: 600;
    overflow-wrap: anywhere;
}

.choice-actions {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    flex: 0 0 auto;
}

.edit-choice {
    padding: 0.15rem 0.5rem;
    border: 1px solid #CE760C;
    border-radius: 4px;
    background: #DFDFA9;
    color: #4f320c;
    cursor: pointer;
    font-size: 0.85em;
}

.edit-choice:hover,
.edit-choice:focus-visible {
    background: #6b6b33;
    color: #fff;
}

/* 折叠态预览：别名/标签本身一直可见，藏起来的只是输入框。
   预览块刻意不带 ×：浏览时不该出现能误删数据的按钮。 */
.choice-preview {
    display: grid;
    gap: 0.15rem;
    margin-top: 0.3rem;
}

.meta-preview-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.25rem;
    font-size: 0.85em;
}

.meta-preview-label {
    color: #6b4a1c;
    font-size: 0.85em;
}

/* 折叠态预览块和展开态标签块的样式都在 .meta-chip 下面，
   改这里的顺序会影响覆盖关系（两边权重相同，后写的生效）。 */

/* 别名和标签：每行一个输入框加若干可以单独删掉的标签块。
   展开的卡片占满整行，所以宽屏下两行并排，不用上下堆着拉长页面。 */
.choice-meta {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.3rem 0.8rem;
    margin-top: 0.4rem;
}

.meta-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.3rem;
    font-size: 0.85em;
}

.meta-label {
    color: #3f2a08;
    font-weight: 600;
}

.meta-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.15rem;
    padding: 0.1rem 0.15rem 0.1rem 0.45rem;
    border-radius: 999px;
    /* 半透明白底压在卡片底色上仍然很亮，深色文字对比度在 7:1 以上。 */
    background: rgba(255, 255, 255, 0.85);
    color: #4f320c;
}

.meta-chip button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    min-height: 20px;
    padding: 0;
    border: 0;
    background: transparent;
    cursor: pointer;
}

.meta-chip .delete-icon {
    color: #8a1a00;
    font-size: 0.95rem;
}

/* 折叠态的只读预览块：没有 ×，所以不需要 .meta-chip 那种右侧留白；
   方角 + 左侧色条，让"类别"读起来是一条色条而不是药丸的圆边。
   必须放在 .meta-chip 之后：两边权重相同，放前面 padding 会被覆盖。 */
.meta-chip-preview {
    padding: 0.05rem 0.4rem;
    border-radius: 4px;
    border-left: 3px solid #CE760C;
}

/* 标签的色条换一个已有的颜色，和别名区分开（不只是靠那两个字）。 */
.meta-preview-tags .meta-chip-preview {
    border-left-color: #6b6b33;
}

.meta-input {
    width: 9rem;
    max-width: 100%;
    padding: 0.15rem 0.35rem;
    font-size: 0.9em;
}

.meta-add {
    padding: 0.15rem 0.5rem;
    border: 1px solid #CE760C;
    border-radius: 4px;
    background: #DFDFA9;
    color: #4f320c;
    cursor: pointer;
    font-size: 0.9em;
}

.meta-add:hover,
.meta-add:focus-visible {
    background: #6b6b33;
    color: #fff;
}

.choice-filter-note {
    max-width: 78rem;
    margin: 0 auto 0.5rem;
    color: #7a4b18;
}

.history-panel {
    margin: 1rem 0;
    padding: 1rem;
    background: rgba(255, 255, 255, 0.25);
    border: 1px solid rgba(95, 56, 12, 0.2);
    border-radius: 8px;
}

.view-note {
    margin-top: 0;
    color: #7a4b18;
}

.statistics-panel,
.backup-panel {
    margin: 1rem 0;
    padding: 1rem;
    background: rgba(255, 255, 255, 0.25);
    border: 1px solid rgba(95, 56, 12, 0.2);
    border-radius: 8px;
}

.backup-panel h2 {
    margin-top: 0;
}

.backup-actions {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.file-button {
    display: inline-block;
    padding: 0.45rem 0.7rem;
    border: 1px solid #CE760C;
    border-radius: 4px;
    background: #DFDFA9;
    color: #4f320c;
    cursor: pointer;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    font-weight: bolder;
}

/* 隐藏的文件输入框被键盘聚焦时，把焦点圈画在可见的标签上。 */
#import-file-input:focus-visible + .file-button {
    outline: 2px solid #6b6b33;
    outline-offset: 2px;
}

.import-preview {
    margin-top: 1rem;
    padding: 0.75rem;
    background: #DFDFA9;
    border-radius: 6px;
}

.statistics-cards {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
}

.statistics-cards div {
    min-width: 8rem;
    padding: 0.75rem;
    background: #DFDFA9;
    border-radius: 6px;
    text-align: center;
}

.statistics-cards strong,
.statistics-cards span {
    display: block;
}

.statistics-cards strong {
    font-size: 1.5rem;
}

/* 排行榜：横向条形图。条形按相对最高次数缩放，榜首满格。 */
.ranking-filters {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    margin: 0.5rem 0;
}

.ranking-filter {
    padding: 0.3rem 0.75rem;
    border: 1px solid #CE760C;
    border-radius: 999px;
    background: #DFDFA9;
    color: #4f320c;
    cursor: pointer;
    font-size: 0.9em;
}

/* #6b6b33 上的白字对比度 5.56:1。 */
.ranking-filter:hover,
.ranking-filter:focus-visible,
.ranking-filter.active {
    background: #6b6b33;
    color: #fff;
}

.ranking-list {
    margin: 0;
    padding-left: 0;
    list-style: none;
}

.ranking-item {
    display: grid;
    grid-template-columns: minmax(4.5rem, 9rem) 1fr auto;
    align-items: center;
    gap: 0.5rem;
    margin: 0.3rem 0;
    color: #4f320c;
}

.ranking-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ranking-bar-track {
    display: block;
    height: 1.1rem;
    background: rgba(255, 255, 255, 0.55);
    border-radius: 999px;
    overflow: hidden;
}

.ranking-bar {
    display: block;
    height: 100%;
    /* 次数为 1 的条目也要看得见一段颜色。 */
    min-width: 3px;
    background: #CE760C;
    border-radius: 999px;
}

.ranking-count {
    font-variant-numeric: tabular-nums;
    font-size: 0.9em;
    white-space: nowrap;
}

.ranking-note {
    margin: 0.5rem 0 0;
    color: #7a4b18;
    font-size: 0.9em;
}

.trend-summary {
    padding: 0.75rem;
    background: #DFDFA9;
    border-radius: 6px;
}

.history-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 0.5rem;
}

.history-header h2 {
    margin: 0;
    font-size: 1rem;
}

.history-list {
    margin: 0;
    padding-left: 1.3rem;
    list-style: disc;
}

.history-list li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    margin: 0.3rem 0;
    color: #4f320c;
}

.history-actions,
.history-editor {
    display: inline-flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.35rem;
}

.history-header button,
.history-actions button,
.history-editor button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 2.25rem;
    padding: 0.3rem 0.6rem;
    line-height: 1.2;
    text-align: center;
}

.history-editor input {
    min-width: 10rem;
    padding: 0.25rem;
    font-size: 1rem;
}

/* 日期时间控件比纯文本框宽（各浏览器还要塞下日历/时钟按钮）。 */
.history-editor input[type="datetime-local"] {
    min-width: 13.5rem;
}

.empty-history {
    display: list-item;
    list-style: none;
    padding-left: 0;
    color: #7a4b18;
}

input {
    padding: 5px;
    font-size: 1.3em;
}

.selection-actions {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem;
    justify-content: center;
    margin: 1rem 0;
}

.selection-actions[hidden] {
    display: none;
}

.selection-actions span {
    flex-basis: 100%;
}

@media (max-width: 760px) {
    .home-choice-layout {
        grid-template-columns: 1fr;
    }
}

/* Small-screen rules must be independent from the desktop breakpoint. */
@media (max-width: 520px) {
    .main-nav {
        overflow-x: auto;
    }

    .nav-button {
        flex: 1 0 auto;
    }

    .history-list li {
        align-items: flex-start;
        flex-direction: column;
    }

    /* 窄屏放不下三列：名字独占一行，条形和次数并排。 */
    .ranking-item {
        grid-template-columns: 1fr auto;
    }

    .ranking-label {
        grid-column: 1 / -1;
    }

    /* 窄屏宁可滚动也不要挤成两列：卡片只留一列，别名和标签也回到上下两行。 */
    .foodlist {
        grid-template-columns: 1fr;
    }

    .choice-meta {
        grid-template-columns: 1fr;
    }

    .meta-input {
        width: 100%;
    }
}

/*MQ For desktop layout*/
@media (min-width:992px) {
    .hori {      
        margin: 25px 0;
    }
}