/* ── MD3 Design Tokens ── */
:root {
  --md-sys-color-primary:                  #9ecaff;
  --md-sys-color-on-primary:               #003354;
  --md-sys-color-primary-container:        #004a77;
  --md-sys-color-on-primary-container:     #cfe5ff;
  --md-sys-color-secondary:                #bbc7db;
  --md-sys-color-on-secondary:             #253140;
  --md-sys-color-secondary-container:      #3b4858;
  --md-sys-color-on-secondary-container:   #d7e3f7;
  --md-sys-color-error:                    #ffb4ab;
  --md-sys-color-on-error:                 #690005;
  --md-sys-color-error-container:          #93000a;
  --md-sys-color-on-error-container:       #ffdad6;
  --md-sys-color-background:               #1a1c1e;
  --md-sys-color-on-background:            #e2e2e6;
  --md-sys-color-surface:                  #1a1c1e;
  --md-sys-color-on-surface:               #e2e2e6;
  --md-sys-color-surface-variant:          #42474e;
  --md-sys-color-on-surface-variant:       #c2c7cf;
  --md-sys-color-outline:                  #8c9198;
  --md-sys-color-outline-variant:          #42474e;
  --md-sys-color-surface-container-low:    #1e2126;
  --md-sys-color-surface-container:        #22262b;
  --md-sys-color-surface-container-high:   #2c3035;
  --md-sys-color-surface-container-highest:#363a3f;

  --md-sys-typescale-headline-small: 24px/32px 'Roboto', sans-serif;
  --md-sys-typescale-title-large:    22px/28px 'Roboto', sans-serif;
  --md-sys-typescale-title-medium:   500 16px/24px 'Roboto', sans-serif;
  --md-sys-typescale-title-small:    500 14px/20px 'Roboto', sans-serif;
  --md-sys-typescale-body-large:     16px/24px 'Roboto', sans-serif;
  --md-sys-typescale-body-medium:    14px/20px 'Roboto', sans-serif;
  --md-sys-typescale-body-small:     12px/16px 'Roboto', sans-serif;
  --md-sys-typescale-label-large:    500 14px/20px 'Roboto', sans-serif;
  --md-sys-typescale-label-medium:   500 12px/16px 'Roboto', sans-serif;
}

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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Roboto', sans-serif;
  background: var(--md-sys-color-background);
  color: var(--md-sys-color-on-surface);
}

/* ── Screens ── */
.screen {
  display: none;
  flex-direction: column;
  height: 100%;
  left: 0;
  position: fixed;
  top: 0;
  width: 100%;
}
.screen.active { display: flex; }

/* ══════════════════════════════════════════
   SIGN-IN SCREEN
══════════════════════════════════════════ */
#s-token {
  align-items: center;
  background: var(--md-sys-color-background);
  gap: 24px;
  justify-content: center;
  overflow-y: auto;
  padding: 32px;
  text-align: center;
}

.signin-icon {
  font-size: 72px;
  color: var(--md-sys-color-primary);
}

.signin-title {
  font: var(--md-sys-typescale-headline-small);
  color: var(--md-sys-color-on-surface);
}

.signin-sub {
  font: var(--md-sys-typescale-body-medium);
  color: var(--md-sys-color-on-surface-variant);
  max-width: 300px;
}

.btn-dropbox {
  align-items: center;
  background: var(--md-sys-color-surface-container-high);
  border: 1px solid var(--md-sys-color-outline);
  border-radius: 24px;
  color: var(--md-sys-color-on-surface);
  cursor: pointer;
  display: inline-flex;
  font: var(--md-sys-typescale-label-large);
  font-size: 15px;
  gap: 12px;
  height: 48px;
  justify-content: center;
  padding: 0 24px;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.btn-dropbox:hover  { background: var(--md-sys-color-surface-container-highest); }
.btn-dropbox:active { opacity: 0.9; }

.setup-note {
  background: rgba(158, 202, 255, 0.07);
  border: 1px solid rgba(158, 202, 255, 0.2);
  border-radius: 12px;
  color: var(--md-sys-color-on-surface-variant);
  font: var(--md-sys-typescale-body-small);
  line-height: 1.8;
  max-width: 400px;
  padding: 12px 16px;
  text-align: left;
}
.setup-note strong { color: var(--md-sys-color-on-surface); }
.setup-note code {
  background: var(--md-sys-color-surface-container-highest);
  border-radius: 4px;
  color: var(--md-sys-color-primary);
  display: inline-block;
  font-size: 11px;
  margin-top: 4px;
  padding: 2px 6px;
  word-break: break-all;
}

/* ══════════════════════════════════════════
   LOADING SCREEN
══════════════════════════════════════════ */
#s-loading {
  align-items: center;
  background: var(--md-sys-color-background);
  justify-content: center;
}

.loader { align-items: center; display: flex; flex-direction: column; gap: 16px; }

.loading-spinner {
  animation: spin 0.8s linear infinite;
  border: 3px solid var(--md-sys-color-outline-variant);
  border-top-color: var(--md-sys-color-primary);
  border-radius: 50%;
  height: 40px;
  width: 40px;
}
@keyframes spin { to { transform: rotate(360deg); } }

#load-msg {
  color: var(--md-sys-color-on-surface-variant);
  font: var(--md-sys-typescale-body-medium);
}

.prog-wrap { align-items: center; display: none; flex-direction: column; gap: 6px; }
.prog-wrap.visible { display: flex; }

.prog-bar {
  background: var(--md-sys-color-outline-variant);
  border-radius: 4px;
  height: 4px;
  overflow: hidden;
  width: 220px;
}
.prog-fill {
  background: var(--md-sys-color-primary);
  border-radius: 4px;
  height: 100%;
  transition: width 0.25s;
  width: 0%;
}
#prog-pct {
  color: var(--md-sys-color-on-surface-variant);
  font: var(--md-sys-typescale-label-medium);
}

/* ══════════════════════════════════════════
   TOP BAR (shared)
══════════════════════════════════════════ */
.top-bar {
  align-items: center;
  background: var(--md-sys-color-surface-container);
  box-shadow: 0 1px 0 var(--md-sys-color-outline-variant);
  display: flex;
  flex-shrink: 0;
  gap: 4px;
  height: 64px;
  padding: 0 4px 0 16px;
  z-index: 10;
}

.top-bar-icon {
  color: var(--md-sys-color-primary);
  font-size: 28px;
}

.top-bar-title {
  color: var(--md-sys-color-on-surface);
  flex: 1;
  font: var(--md-sys-typescale-title-large);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.top-bar-app-title {
  color: var(--md-sys-color-on-surface);
  font: var(--md-sys-typescale-title-large);
  margin-right: 12px;
  white-space: nowrap;
}

/* ── Icon Button ── */
.icon-btn {
  align-items: center;
  background: transparent;
  border: none;
  border-radius: 50%;
  color: var(--md-sys-color-on-surface-variant);
  cursor: pointer;
  display: flex;
  flex-shrink: 0;
  height: 48px;
  justify-content: center;
  transition: background 0.15s;
  width: 48px;
  -webkit-tap-highlight-color: transparent;
}

/* Right-side action group — always pushed to the far right */
.top-bar-right {
  align-items: center;
  display: flex;
  flex-shrink: 0;
  gap: 0;
  margin-left: auto;
}
.icon-btn:hover  { background: rgba(194, 199, 207, 0.08); }
.icon-btn:active { background: rgba(194, 199, 207, 0.12); }
.icon-btn .material-symbols-outlined { font-size: 24px; }

/* ── Chip ── */
.chip {
  align-items: center;
  background: transparent;
  border: 1px solid var(--md-sys-color-outline);
  border-radius: 8px;
  color: var(--md-sys-color-on-surface-variant);
  cursor: pointer;
  display: inline-flex;
  flex-shrink: 0;
  font: var(--md-sys-typescale-label-large);
  gap: 4px;
  height: 32px;
  padding: 0 12px;
  transition: all 0.15s;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.chip:hover { background: rgba(194, 199, 207, 0.08); }
.chip.selected,
.chip.active {
  background: var(--md-sys-color-secondary-container);
  border-color: transparent;
  color: var(--md-sys-color-on-secondary-container);
}

/* ══════════════════════════════════════════
   BROWSER SCREEN
══════════════════════════════════════════ */
#s-browser { background: var(--md-sys-color-background); }

.breadcrumb {
  color: var(--md-sys-color-on-surface-variant);
  flex: 1;
  font: var(--md-sys-typescale-body-medium);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.crumb { cursor: pointer; transition: color 0.15s; }
.crumb:hover   { color: var(--md-sys-color-on-surface); }
.crumb.current { color: var(--md-sys-color-on-surface); cursor: default; }
.sep { color: var(--md-sys-color-outline); margin: 0 4px; }

.search-input {
  background: var(--md-sys-color-surface-container-highest);
  border: 1px solid var(--md-sys-color-outline);
  border-radius: 8px;
  color: var(--md-sys-color-on-surface);
  font: var(--md-sys-typescale-body-medium);
  outline: none;
  padding: 6px 12px;
  transition: border-color 0.15s;
  width: 180px;
  -webkit-tap-highlight-color: transparent;
}
.search-input:focus { border-color: var(--md-sys-color-primary); }
.search-input::placeholder { color: var(--md-sys-color-on-surface-variant); }

/* ── Browser Tab chip row ── */
.chip-row {
  align-items: center;
  background: var(--md-sys-color-surface-container);
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
  display: flex;
  flex-shrink: 0;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 16px 12px;
  scrollbar-color: var(--md-sys-color-outline) transparent;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}
.chip-row::-webkit-scrollbar { height: 4px; }
.chip-row::-webkit-scrollbar-track { background: transparent; }
.chip-row::-webkit-scrollbar-thumb { background: var(--md-sys-color-outline); border-radius: 2px; }
@media (hover: none) {
  .chip-row { scrollbar-width: none; }
  .chip-row::-webkit-scrollbar { display: none; }
}

/* ── File Grid ── */
#file-grid {
  align-content: start;
  display: grid;
  flex: 1;
  gap: 8px;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  overflow-y: auto;
  padding: 12px 16px;
}

/* ── File Card ── */
.file-card {
  align-items: center;
  background: var(--md-sys-color-surface-container);
  border-radius: 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 16px 10px 12px;
  position: relative;
  text-align: center;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.file-card:hover  { background: var(--md-sys-color-surface-container-high); }
.file-card:active { background: var(--md-sys-color-surface-container-highest); }

.file-icon .material-symbols-outlined {
  color: var(--md-sys-color-on-surface-variant);
  font-size: 40px;
}

.file-name {
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  color: var(--md-sys-color-on-surface);
  display: -webkit-box;
  font: var(--md-sys-typescale-body-small);
  line-height: 1.4;
  overflow: hidden;
  word-break: break-word;
}

.file-badge {
  background: var(--md-sys-color-secondary-container);
  border-radius: 4px;
  color: var(--md-sys-color-on-secondary-container);
  font: var(--md-sys-typescale-label-medium);
  padding: 2px 6px;
  position: absolute;
  right: 8px;
  top: 8px;
}
.badge-pdf {
  background: var(--md-sys-color-primary-container);
  color: var(--md-sys-color-on-primary-container);
}

.empty {
  color: var(--md-sys-color-on-surface-variant);
  font: var(--md-sys-typescale-body-medium);
  grid-column: 1 / -1;
  padding: 64px 24px;
  text-align: center;
}

/* ── Library Cards ── */
.lib-progress {
  align-items: center;
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 100%;
}

.lib-prog-bar {
  background: var(--md-sys-color-surface-container-highest);
  border-radius: 3px;
  height: 4px;
  overflow: hidden;
  width: 100%;
}
.lib-prog-fill {
  background: var(--md-sys-color-primary);
  border-radius: 3px;
  height: 100%;
}
.lib-prog-text {
  color: var(--md-sys-color-on-surface-variant);
  font: var(--md-sys-typescale-label-medium);
}

.lib-remove {
  align-items: center;
  background: transparent;
  border: none;
  border-radius: 50%;
  color: var(--md-sys-color-on-surface-variant);
  cursor: pointer;
  display: flex;
  height: 28px;
  justify-content: center;
  position: absolute;
  right: 4px;
  top: 4px;
  transition: background 0.15s, color 0.15s;
  width: 28px;
  z-index: 2;
  -webkit-tap-highlight-color: transparent;
}
.lib-remove:hover {
  background: var(--md-sys-color-error-container);
  color: var(--md-sys-color-on-error-container);
}
.lib-remove .material-symbols-outlined { font-size: 16px; }

/* ══════════════════════════════════════════
   READER SCREEN
══════════════════════════════════════════ */
#s-reader { background: #000; }

.reader-toolbar {
  backdrop-filter: blur(12px);
  background: rgba(26, 28, 30, 0.92);
  flex-wrap: wrap;
  gap: 2px;
  height: auto;
  min-height: 56px;
  padding: 8px 4px;
  transition: opacity 0.35s;
}
.reader-toolbar.hidden { opacity: 0; pointer-events: none; }

.zoom-display {
  color: var(--md-sys-color-on-surface-variant);
  font: var(--md-sys-typescale-label-medium);
  min-width: 40px;
  text-align: center;
}

.page-counter {
  color: var(--md-sys-color-on-surface-variant);
  font: var(--md-sys-typescale-label-medium);
  min-width: 60px;
  text-align: center;
}

/* ── Page Area ── */
#page-area {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

.nav-zone {
  cursor: pointer;
  height: 100%;
  position: absolute;
  top: 0;
  transition: background 0.2s;
  width: 28%;
  z-index: 5;
}
.nav-zone:hover { background: rgba(158, 202, 255, 0.04); }
.nav-zone.left  { left: 0; }
.nav-zone.right { right: 0; }

.nav-zone::after {
  background: var(--md-sys-color-primary);
  border-radius: 3px;
  content: '';
  height: 48px;
  left: 10px;
  opacity: 0;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  transition: opacity 0.2s;
  width: 3px;
}
.nav-zone.right::after { left: auto; right: 10px; }
.nav-zone:hover::after { opacity: 0.45; }

/* ── Pages ── */
.page-container {
  align-items: center;
  display: flex;
  flex: 1;
  gap: 4px;
  height: 100%;
  justify-content: center;
  overflow: hidden;
  padding: 8px;
  position: relative;
  z-index: 1;
}

.comic-page {
  display: block;
  flex-shrink: 0;
  height: auto;
  max-height: 100%;
  max-width: 100%;
  object-fit: contain;
  width: auto;
}

.page-container.double .comic-page { max-width: calc(50% - 2px); }

.page-loading {
  align-items: center;
  background: var(--md-sys-color-surface-container);
  border-radius: 8px;
  color: var(--md-sys-color-on-surface-variant);
  display: flex;
  flex-shrink: 0;
  font: var(--md-sys-typescale-body-medium);
  height: 80%;
  justify-content: center;
  min-width: 200px;
  width: 60%;
}

/* ══════════════════════════════════════════
   LIST VIEW
══════════════════════════════════════════ */
#file-grid.list-view {
  grid-template-columns: 1fr;
  gap: 2px;
  padding: 8px 16px;
}

#file-grid.list-view .file-card {
  flex-direction: row;
  align-items: center;
  gap: 14px;
  padding: 10px 14px;
  border-radius: 8px;
  text-align: left;
}

/* Reorder children in list row */
#file-grid.list-view .file-icon        { order: 1; flex-shrink: 0; }
#file-grid.list-view .file-name        { order: 2; flex: 1; text-align: left; -webkit-line-clamp: 1; font: var(--md-sys-typescale-body-medium); }
#file-grid.list-view .file-badge       { order: 3; position: static; flex-shrink: 0; }
#file-grid.list-view .lib-progress     { order: 4; flex-direction: row; width: auto; flex-shrink: 0; align-items: center; gap: 8px; }
#file-grid.list-view .lib-remove       { order: 5; position: static; flex-shrink: 0; }

/* Smaller icon in list mode */
#file-grid.list-view .file-icon .material-symbols-outlined { font-size: 22px; }

/* Compact progress bar in list mode */
#file-grid.list-view .lib-prog-bar     { width: 64px; flex-shrink: 0; }
#file-grid.list-view .lib-prog-text    { white-space: nowrap; min-width: 52px; }

/* ══════════════════════════════════════════
   PROFILE SCREEN
══════════════════════════════════════════ */
#s-profile {
  align-items: center;
  background: var(--md-sys-color-background);
  gap: 24px;
  justify-content: center;
  overflow-y: auto;
  padding: 32px;
  text-align: center;
}

/* Grid of profile cards */
.profile-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 8px;
  max-width: 320px;
  width: 100%;
}

/* Individual profile card button */
.profile-card {
  align-items: center;
  background: var(--md-sys-color-surface-container);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: 16px;
  color: var(--md-sys-color-on-surface);
  cursor: pointer;
  display: flex;
  font: var(--md-sys-typescale-title-medium);
  gap: 14px;
  padding: 18px 20px;
  text-align: left;
  transition: background 0.15s, border-color 0.15s;
  width: 100%;
  -webkit-tap-highlight-color: transparent;
}
.profile-card:hover {
  background: var(--md-sys-color-surface-container-high);
  border-color: var(--md-sys-color-primary);
}
.profile-card:active { background: var(--md-sys-color-surface-container-highest); }

/* Avatar circle */
.profile-avatar {
  align-items: center;
  background: var(--md-sys-color-primary-container);
  border-radius: 50%;
  color: var(--md-sys-color-on-primary-container);
  display: flex;
  flex-shrink: 0;
  font: 700 18px/1 'Roboto', sans-serif;
  height: 44px;
  justify-content: center;
  width: 44px;
}

/* ── Profile pill button in top bar ── */
.profile-pill {
  align-items: center;
  background: transparent;
  border: none;
  border-radius: 20px;
  color: var(--md-sys-color-on-surface-variant);
  cursor: pointer;
  display: flex;
  font: var(--md-sys-typescale-label-large);
  gap: 4px;
  height: 40px;
  padding: 0 10px;
  transition: background 0.15s;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.profile-pill:hover  { background: rgba(194, 199, 207, 0.08); }
.profile-pill:active { background: rgba(194, 199, 207, 0.12); }

/* ── Profile dropdown menu ── */
.profile-menu {
  background: var(--md-sys-color-surface-container-high);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  display: none;
  min-width: 180px;
  overflow: hidden;
  padding: 4px 0;
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  z-index: 200;
}
.profile-menu.open { display: block; }

.profile-menu-item {
  align-items: center;
  background: transparent;
  border: none;
  color: var(--md-sys-color-on-surface);
  cursor: pointer;
  display: flex;
  font: var(--md-sys-typescale-body-medium);
  gap: 12px;
  padding: 12px 16px;
  text-align: left;
  transition: background 0.15s;
  width: 100%;
  -webkit-tap-highlight-color: transparent;
}
.profile-menu-item:hover { background: var(--md-sys-color-surface-container-highest); }
.profile-menu-item.active { color: var(--md-sys-color-primary); font-weight: 600; }
.profile-menu-item .material-symbols-outlined { font-size: 18px; }
.profile-menu-divider {
  background: var(--md-sys-color-outline-variant);
  height: 1px;
  margin: 4px 0;
}

/* ── Responsive ── */
@media (max-width: 600px) {
  .search-input { width: 120px; }
  #reader-title { display: none; }
}
