:root {
  --bg: #f5f5f5;
  --fg: #1a1a1a;
  --accent: #e8590c;
  --panel: #ffffff;
  --border: #ddd;
  --muted: #666;
  --reader-bg: #ffffff;
  --reader-container-width: 30;
  --reader-orp-target: 15;
  --reader-font-scale: 1;
}

.theme-dark {
  --bg: #121212;
  --fg: #e0e0e0;
  --accent: #ff922b;
  --panel: #1e1e1e;
  --border: #333;
  --muted: #999;
  --reader-bg: #0a0a0a;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

header {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 1rem 2rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

header h1 {
  font-size: 1.25rem;
  font-weight: 600;
}

header .actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

button, .btn {
  background: var(--fg);
  color: var(--bg);
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

button.secondary, .btn.secondary {
  background: var(--panel);
  color: var(--fg);
  border: 1px solid var(--border);
}

button:hover, .btn:hover {
  opacity: 0.9;
}

main {
  flex: 1;
  padding: 2rem;
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
}

/* Book list */
.book-list {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
}

.book-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.1s;
  position: relative;
}

.book-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}

.book-card .title {
  font-weight: 600;
  font-size: 0.8rem;
  margin-bottom: 0.25rem;
  word-break: break-word;
}

.book-card .meta {
  color: var(--muted);
  font-size: 0.65rem;
}

.book-card .delete-btn {
  position: absolute;
  top: 0.25rem;
  right: 0.25rem;
  background: transparent;
  color: var(--muted);
  border: none;
  padding: 0.15rem;
  font-size: 0.85rem;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s;
}

.book-card:hover .delete-btn {
  opacity: 1;
}

.book-card .delete-btn:hover {
  color: #e03131;
}

/* Upload area */
.upload-area {
  border: 2px dashed var(--border);
  border-radius: 10px;
  padding: 2rem;
  text-align: center;
  margin-bottom: 2rem;
  background: var(--panel);
  transition: border-color 0.2s;
}

.upload-area.dragover {
  border-color: var(--accent);
}

.upload-area input[type="file"] {
  display: none;
}

.upload-area label {
  display: inline-block;
  cursor: pointer;
  color: var(--accent);
  font-weight: 500;
}

/* Reader overlay */
.reader-overlay {
  position: fixed;
  inset: 0;
  background: var(--reader-bg);
  display: none;
  flex-direction: column;
  z-index: 100;
}

.reader-overlay.active {
  display: flex;
}

.reader-toolbar {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.reader-toolbar .book-title {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 300px;
}

.reader-toolbar .controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.reader-toolbar .controls label {
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
}

#speed-slider {
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  color: var(--fg);
  font-size: 0.85rem;
  cursor: pointer;
}

.reader-stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  position: relative;
  cursor: pointer;
}

.reader-word-container {
  display: inline-block;
  width: calc(var(--reader-container-width) * 1ch);
  max-width: 100%;
  text-align: left;
  font-family: "SF Mono", Monaco, "Cascadia Code", "Roboto Mono", Consolas, "Courier New", monospace;
  font-size: calc(var(--reader-font-scale) * clamp(2rem, 5vw, 4rem));
  font-weight: 500;
  position: relative;
  z-index: 2;
}

.focus-marker {
  position: absolute;
  left: calc(var(--reader-orp-target) * 1ch + 0.5ch);
  transform: translateX(-50%);
  width: 0;
  height: 0;
  pointer-events: none;
  z-index: 1;
}

.focus-marker.top {
  top: -16px;
  border-left: 14px solid transparent;
  border-right: 14px solid transparent;
  border-top: 20px solid rgba(255, 255, 255, 0.7);
}

.focus-marker.bottom {
  bottom: -16px;
  border-left: 14px solid transparent;
  border-right: 14px solid transparent;
  border-bottom: 20px solid rgba(255, 255, 255, 0.7);
}

.reader-word {
  white-space: pre;
}

.reader-word .orp {
  color: #ff1a1a;
  font-weight: 700;
}

.warmup-indicator {
  font-size: 0.8rem;
  color: var(--accent);
  font-weight: 600;
  animation: pulse 1s ease-in-out infinite;
}

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

.reader-progress {
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding: 0.75rem 1.5rem;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  align-items: center;
  gap: 1rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.reader-progress #time-remaining {
  text-align: left;
}

.reader-progress #word-index {
  text-align: center;
}

.reader-progress input[type="range"] {
  grid-column: 1 / -1;
}

/* Empty state */
.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 3rem 1rem;
}

.empty-state h2 {
  color: var(--fg);
  margin-bottom: 0.5rem;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: none;
  align-items: center;
  justify-content: center;
}

.modal.active {
  display: flex;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
}

.modal-panel {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 100%;
  max-width: 400px;
  margin: 1rem;
  z-index: 1;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  font-size: 1.1rem;
  font-weight: 600;
}

.modal-close {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1.4rem;
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
}

.modal-close:hover {
  background: var(--bg);
  color: var(--fg);
}

.modal-body {
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.setting-row {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.setting-row label {
  font-size: 0.9rem;
  color: var(--muted);
}

.setting-row input[type="number"] {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  font-size: 1rem;
  width: 100%;
}

.modal-footer {
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
}

.font-size-control {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.font-size-control span {
  min-width: 3.5rem;
  text-align: center;
  font-weight: 600;
  font-size: 1rem;
  color: var(--fg);
}

.font-size-control button {
  width: 36px;
  height: 36px;
  padding: 0;
  justify-content: center;
  font-size: 1.2rem;
  line-height: 1;
}

.book-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}

.search-box {
  flex: 1;
  min-width: 200px;
}

.search-box input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  color: var(--fg);
  font-size: 0.95rem;
}

.search-box input:focus {
  outline: none;
  border-color: var(--accent);
}

.sort-box {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  white-space: nowrap;
}

.sort-box label {
  font-size: 0.9rem;
  color: var(--muted);
}

.sort-box select {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  color: var(--fg);
  font-size: 0.9rem;
  cursor: pointer;
}

.book-card .cover {
  width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  border-radius: 4px;
  margin-bottom: 0.75rem;
  background: var(--bg);
  display: block;
}

.book-card .cover-placeholder {
  width: 100%;
  aspect-ratio: 2 / 3;
  border-radius: 4px;
  margin-bottom: 0.75rem;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.85rem;
}

/* Desktop */
@media (min-width: 601px) {
  .book-list {
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 1rem;
  }

  .book-card {
    padding: 1.25rem;
  }

  .book-card .title {
    font-size: 1.05rem;
  }

  .book-card .meta {
    font-size: 0.85rem;
  }

  .book-card .delete-btn {
    top: 0.75rem;
    right: 0.75rem;
    padding: 0.25rem;
    font-size: 1.1rem;
  }
}

/* Mobile responsiveness */
@media (max-width: 600px) {
  :root {
    --reader-container-width: 18;
    --reader-orp-target: 9;
  }

  header {
    padding: 0.75rem 1rem;
  }

  header h1 {
    font-size: 1.1rem;
  }

  main {
    padding: 1rem;
  }

  .upload-area {
    padding: 1.25rem;
    font-size: 0.9rem;
  }

  .reader-toolbar {
    padding: 0.5rem 0.75rem;
    gap: 0.5rem;
  }

  .reader-toolbar .book-title {
    font-size: 0.85rem;
    max-width: 200px;
  }

  .reader-toolbar .controls {
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.4rem;
    width: 100%;
  }

  #speed-slider {
    font-size: 0.8rem;
    padding: 0.25rem 0.4rem;
  }

  .reader-stage {
    padding: 0.5rem;
  }

  .reader-word-container {
    font-size: calc(var(--reader-font-scale) * clamp(1.25rem, 5vw, 2rem));
  }

  .focus-marker.top {
    top: -12px;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-top: 14px solid rgba(255, 255, 255, 0.7);
  }

  .focus-marker.bottom {
    bottom: -12px;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-bottom: 14px solid rgba(255, 255, 255, 0.7);
  }

  .reader-progress {
    padding: 0.5rem 0.75rem;
    font-size: 0.8rem;
    gap: 0.5rem;
  }

  .modal-panel {
    margin: 0.5rem;
  }

  .book-controls {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
  }

  .search-box {
    min-width: auto;
  }

  .sort-box {
    justify-content: flex-end;
  }
}

/* Touch devices: always show delete button */
@media (hover: none) {
  .book-card .delete-btn {
    opacity: 1;
  }
}
