/* =========================================================
   English Reader · Mobile layout (≤767.98px)
   Based on docs/MOBILE_DESIGN.md + mobile-preview.html
   Desktop (≥768) unchanged
   ========================================================= */

html.is-mobile,
html.is-mobile body {
  height: 100%;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ---- Shell: tab bar ---- */
#mobileTabBar {
  display: none;
}
#erSheetRoot {
  display: none;
}
#erToast {
  display: none;
}

@media (max-width: 767.98px) {
  #mobileTabBar {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(64px + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    z-index: 900;
    align-items: center;
    justify-content: space-around;
    border-top: 1px solid var(--border);
    background: var(--surface);
    box-sizing: border-box;
  }
  .tab-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    flex: 1;
    min-height: 44px;
    min-width: 44px;
    padding: 6px 0 4px;
    color: var(--text-3);
    font-size: 10px;
    text-decoration: none;
    position: relative;
    background: none;
    border: none;
    font-family: inherit;
    cursor: pointer;
  }
  .tab-item svg {
    width: 22px;
    height: 22px;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
  }
  .tab-item.active {
    color: var(--accent);
  }
  .tab-item .tab-badge {
    position: absolute;
    top: 2px;
    right: calc(50% - 18px);
    background: var(--danger);
    color: #fff;
    font-size: 9px;
    font-weight: 600;
    padding: 1px 5px;
    border-radius: 8px;
    min-width: 14px;
    text-align: center;
    line-height: 1.3;
  }
  .tab-item .tab-badge[hidden] {
    display: none;
  }

  /* Theme switch: lift above tab bar */
  .theme-switch {
    bottom: calc(72px + env(safe-area-inset-bottom, 0px));
    right: 12px;
    transform: scale(0.92);
    transform-origin: bottom right;
  }

  /* App container */
  #app.app,
  #app.app.full,
  #app.app.no-drawer,
  #app.app.no-sidebar {
    display: flex !important;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    width: 100%;
    overflow: hidden;
    grid-template-columns: none !important;
    grid-template-rows: none !important;
  }

  /* Hide desktop chrome */
  #app .titlebar .nav,
  #app .titlebar .brand,
  #app .titlebar .streak {
    display: none !important;
  }
  #app .titlebar {
    height: 48px;
    min-height: 48px;
    padding: 0 12px;
    flex-shrink: 0;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
  }
  #app .titlebar .right {
    margin-left: 0;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
  }
  #app .titlebar .right::before {
    content: attr(data-mobile-title);
    font-size: 16px;
    font-weight: 600;
    color: var(--text);
  }
  #app[data-m-route='library'] .titlebar .right::before {
    content: '我的书库';
  }
  #app[data-m-route='reader'] .titlebar .right::before {
    content: '阅读';
  }
  #app[data-m-route='vocab'] .titlebar .right::before {
    content: '生词本';
  }
  #app[data-m-route='practice'] .titlebar .right::before {
    content: '练习中心';
  }
  #app[data-m-route='review'] .titlebar .right::before {
    content: '单词复习';
  }
  #app[data-m-route='stats'] .titlebar .right::before {
    content: '学习统计';
  }
  #app[data-m-route='settings'] .titlebar .right::before {
    content: '我的';
  }
  #app[data-m-route='ai'] .titlebar .right::before {
    content: 'AI 练习';
  }
  #app[data-m-route='translate'] .titlebar .right::before {
    content: '翻译队列';
  }

  #app .sidebar,
  #app .drawer {
    display: none !important;
  }

  #app .main,
  #app #readerMain {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px 16px calc(80px + env(safe-area-inset-bottom, 0px)) !important;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  /* ---- Library ---- */
  .library-header {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    margin-bottom: 12px;
  }
  .library-header h1 {
    font-size: 18px;
    display: none; /* title in app-bar */
  }
  .library-grid {
    grid-template-columns: 1fr !important;
    gap: 10px;
  }
  .book-card {
    display: flex;
    flex-direction: column;
    border-radius: var(--radius-lg);
    padding: 14px;
  }
  .book-card .cover-art {
    width: 100%;
    height: 72px;
    border-radius: var(--radius-sm);
    margin-bottom: 10px;
  }
  .book-card .card-actions .btn {
    min-height: 44px;
    flex: 1;
  }
  .drop-zone {
    margin-top: 12px;
    padding: 20px 14px;
    border-radius: var(--radius-lg);
  }
  #translateQueueBar {
    border-radius: var(--radius-lg) !important;
    margin-bottom: 12px !important;
  }

  /* ---- Reader ---- */
  .reader-sticky {
    position: sticky;
    top: 0;
    z-index: 20;
    background: var(--bg);
    padding-bottom: 8px;
  }
  .reader-sticky-info h1 {
    font-size: 15px;
  }
  .reader-controls {
    flex-wrap: wrap;
    gap: 4px;
  }
  .reader-ctrl-hint,
  .kbd {
    display: none;
  }
  #readerContent {
    font-size: 15px;
    line-height: 1.85;
    padding-bottom: 120px;
  }
  .paragraph .zh {
    font-size: 13px;
  }

  /* Dict as bottom sheet on mobile */
  .dict-popover {
    position: fixed !important;
    left: 12px !important;
    right: 12px !important;
    bottom: calc(76px + env(safe-area-inset-bottom, 0px)) !important;
    top: auto !important;
    width: auto !important;
    max-width: none !important;
    max-height: 45vh;
    overflow-y: auto;
    border-radius: var(--radius-lg) !important;
    box-shadow: var(--shadow-md) !important;
    z-index: 950 !important;
  }
  .dict-popover .actions .btn {
    flex: 1;
    min-height: 44px;
  }

  /* Page words drawer → floating chip opens sheet via JS class */
  .m-page-words-fab {
    display: flex !important;
    position: fixed;
    right: 12px;
    bottom: calc(80px + env(safe-area-inset-bottom, 0px));
    z-index: 850;
    min-height: 44px;
    padding: 0 14px;
    border-radius: 22px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--accent);
    font-size: 12px;
    font-weight: 500;
    align-items: center;
    gap: 6px;
    box-shadow: var(--shadow-md);
    font-family: inherit;
  }

  /* ---- Vocab ---- */
  #app[data-m-route='vocab'] .main > h1 {
    display: none;
  }
  #app[data-m-route='vocab'] .toolbar {
    flex-wrap: wrap;
    gap: 8px;
  }
  #app[data-m-route='vocab'] #vocabSearch {
    width: 100% !important;
    min-height: 44px;
    font-size: 14px !important;
    border-radius: var(--radius-lg) !important;
  }
  #vocabList {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }
  .word-card {
    border-radius: var(--radius-lg);
    padding: 12px;
  }

  /* ---- Practice hub ---- */
  #app[data-m-route='practice'] .main > h1,
  #app[data-m-route='practice'] .main > .meta {
    display: none;
  }
  .practice-section {
    margin: 0 0 16px;
    padding: 0;
    border: none;
    background: transparent;
  }
  .practice-section-title {
    font-size: 13px;
    margin: 0 0 8px;
  }
  .practice-daily-card {
    background: linear-gradient(135deg, var(--accent-soft), var(--practice-ai-soft));
    border: 1px solid var(--accent);
    border-radius: var(--radius-lg);
    padding: 14px;
    margin-bottom: 4px;
  }
  .practice-daily-card h3 {
    font-size: 15px;
    margin: 0 0 4px;
  }
  .practice-daily-card .desc {
    font-size: 11px;
    color: var(--text-2);
    margin: 0 0 10px;
  }
  .practice-daily-card .btn {
    width: 100%;
    min-height: 44px;
  }
  .practice-section-word .practice-grid,
  .word-mode-grid {
    display: grid !important;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }
  .practice-card-word {
    background: var(--practice-word-soft) !important;
    border: 1px solid var(--practice-word) !important;
    border-radius: var(--radius-lg) !important;
    padding: 12px !important;
    margin: 0 !important;
  }
  .practice-card-word h3 {
    font-size: 13px !important;
    color: var(--practice-word) !important;
    margin: 0 0 4px !important;
  }
  .practice-card-word p,
  .practice-card-word .badge-due {
    font-size: 10px !important;
  }
  .practice-card-word .btn {
    min-height: 40px;
    font-size: 12px;
    padding: 6px 8px;
  }
  .practice-section-ai {
    background: transparent !important;
    border: none !important;
    padding: 0 !important;
  }
  .practice-section-ai .practice-grid {
    display: flex !important;
    flex-direction: column;
    gap: 8px;
  }
  .practice-card-ai {
    background: var(--practice-ai-soft) !important;
    border: 1px solid var(--practice-ai) !important;
    border-radius: var(--radius-lg) !important;
    padding: 12px !important;
    margin: 0 !important;
    display: flex;
    flex-direction: column;
    gap: 4px;
  }
  .practice-card-ai h3 {
    font-size: 13px !important;
    color: var(--practice-ai) !important;
  }
  .practice-card-ai .btn {
    align-self: flex-start;
    min-height: 40px;
    border-color: var(--practice-ai);
    color: var(--practice-ai);
  }
  .practice-card-ai .btn.primary {
    background: var(--practice-ai);
    color: #fff;
    border-color: var(--practice-ai);
  }

  /* ---- Review ---- */
  .review {
    padding: 8px 0 24px !important;
    max-width: none !important;
  }
  .review-locked-head {
    margin-bottom: 8px;
  }
  .review-locked-title h2 {
    font-size: 16px;
  }
  .review .stats-bar {
    margin-bottom: 10px;
  }
  .review .stats-bar .num {
    font-size: 16px;
  }
  .review .card {
    border-radius: var(--radius-lg);
    padding: 20px 14px;
  }
  .review .word {
    font-size: 28px !important;
  }
  .review .option {
    min-height: 44px;
    padding: 10px 12px;
  }
  .review .rating-bar {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
    margin-top: 16px !important;
  }
  .review .rating-bar .btn {
    min-width: 0 !important;
    min-height: 52px;
    padding: 10px 8px !important;
    border-radius: 8px;
  }

  /* ---- AI pages ---- */
  #app[data-m-route='ai'] .main {
    display: flex;
    flex-direction: column;
  }
  .ai-chat-messages,
  #aiMessages,
  #chatMessages {
    flex: 1;
  }
  .voice-record-btn,
  #voiceHoldBtn,
  .m-voice-btn {
    width: 88px;
    height: 88px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    border: none;
    box-shadow: 0 4px 20px color-mix(in srgb, var(--accent) 40%, transparent);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    transition: transform 0.15s, background 0.15s;
  }
  .voice-record-btn.recording,
  #voiceHoldBtn.recording,
  .m-voice-btn.recording {
    transform: scale(0.9);
    background: var(--danger);
  }

  .translate-queue-panel .tq-row {
    flex-direction: column;
  }
  .translate-queue-panel .tq-actions {
    flex-direction: row;
    width: 100%;
  }
  .translate-queue-panel .tq-act {
    flex: 1;
    min-height: 40px;
  }
  .translate-queue-panel .tq-name {
    max-width: 160px;
  }

  .translate-page-head { flex-direction: column; gap: 10px; }
  .translate-page-head #translateBackLib { align-self: flex-start; order: -1; }
  .tq-section { padding: 12px; }
  .tq-card {
    flex-direction: column;
    align-items: stretch;
  }
  .tq-actions {
    flex-direction: row;
    width: 100%;
    justify-content: stretch;
  }
  .tq-act { flex: 1; min-height: 40px; }
  .tq-msg { white-space: normal; }
  .drop-zone { display: none !important; }
  #app[data-m-route='library'] .titlebar .right .translate-entry {
    display: inline-flex !important;
    padding: 0 8px !important;
    height: 32px;
  }
  .translate-entry-label { display: none; }

  /* ---- Settings / Me ---- */
  #app[data-m-route='settings'] .main > h1,
  #app[data-m-route='stats'] .main > h1 {
    font-size: 18px;
  }
  #app .settings-wrap {
    flex: 1;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100% !important;
    width: 100%;
    margin: 0 !important;
    padding: 12px 16px calc(80px + env(safe-area-inset-bottom, 0px)) !important;
    box-sizing: border-box;
  }
  .settings-section {
    border-radius: var(--radius-lg);
    padding: 12px !important;
    margin-bottom: 10px;
    overflow: hidden;
    max-width: 100%;
    box-sizing: border-box;
  }
  .settings-section .section-desc {
    word-break: break-word;
    overflow-wrap: anywhere;
  }
  .settings-section .section-desc code {
    word-break: break-all;
  }
  .form-row {
    flex-wrap: wrap;
    align-items: flex-start !important;
    gap: 8px !important;
  }
  .form-row .label {
    flex: 1 1 100%;
    min-width: 0;
  }
  .form-row .control {
    flex: 1 1 100%;
    min-width: 0;
    max-width: 100%;
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
  }
  .form-row .control select,
  .form-row .control input[type='text'],
  .form-row .control input[type='password'] {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    box-sizing: border-box;
    font-size: 13px !important;
  }
  .form-row .control .btn {
    min-height: 40px;
  }
  .stats-row,
  .stat-cards {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
  }
  .stat-card,
  .card-block {
    border-radius: var(--radius-lg);
  }
  #app[data-m-route='stats'] .main > div[style*='grid-template-columns'] {
    grid-template-columns: 1fr !important;
  }

  /* Me hub links */
  .m-me-nav {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 16px;
  }
  .m-me-nav a {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px;
    min-height: 44px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    color: var(--text);
    text-decoration: none;
    font-size: 14px;
  }
  .m-me-nav a span {
    color: var(--text-3);
    font-size: 12px;
  }

  /* Sheet / Toast */
  #erSheetRoot {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 1200;
    pointer-events: none;
  }
  #erSheetRoot.open {
    pointer-events: auto;
  }
  #erSheetRoot .er-sheet-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    opacity: 0;
    transition: opacity 0.2s;
  }
  #erSheetRoot.open .er-sheet-backdrop {
    opacity: 1;
  }
  #erSheetRoot .er-sheet {
    position: absolute;
    left: 12px;
    right: 12px;
    bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 16px;
    box-shadow: var(--shadow-md);
    transform: translateY(120%);
    transition: transform 0.22s ease;
  }
  #erSheetRoot.open .er-sheet {
    transform: translateY(0);
  }
  #erSheetRoot .er-sheet-title {
    font-size: 15px;
    font-weight: 600;
    margin-bottom: 8px;
  }
  #erSheetRoot .er-sheet-body {
    font-size: 13px;
    color: var(--text-2);
    margin-bottom: 14px;
    line-height: 1.5;
  }
  #erSheetRoot .er-sheet-actions {
    display: flex;
    gap: 8px;
  }
  #erSheetRoot .er-sheet-actions .btn {
    flex: 1;
    min-height: 44px;
  }

  #erToast {
    display: block;
    position: fixed;
    left: 50%;
    bottom: calc(88px + env(safe-area-inset-bottom, 0px));
    transform: translateX(-50%) translateY(20px);
    background: var(--text);
    color: var(--surface);
    padding: 10px 16px;
    border-radius: 10px;
    font-size: 13px;
    z-index: 1300;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s, transform 0.2s;
    max-width: calc(100% - 32px);
    text-align: center;
  }
  #erToast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }

  /* Hide tab bar on secondary flows that need focus? keep for hub tabs only —
     review/ai still show bar but dim — design keeps tab for hub; hide on review/ai for focus */
  html.is-mobile[data-focus-flow='1'] #mobileTabBar {
    display: none;
  }
  html.is-mobile[data-focus-flow='1'] #app .main,
  html.is-mobile[data-focus-flow='1'] #app #readerMain {
    padding-bottom: calc(24px + env(safe-area-inset-bottom, 0px)) !important;
  }
  html.is-mobile[data-focus-flow='1'] .theme-switch {
    bottom: 16px;
  }
  html.is-mobile[data-focus-flow='1'] .dict-popover {
    bottom: 12px !important;
  }
}

@media (min-width: 768px) {
  #mobileTabBar,
  .m-page-words-fab,
  .m-me-nav {
    display: none !important;
  }
}
