:root {
  --color-bg-app: #fafafa;
  --color-bg-surface: #ffffff;
  --color-bg-surface-alt: #f5f5f5;
  --color-border: #e5e5e5;
  --color-border-hover: #d4d4d4;

  --color-text-primary: #171717;
  --color-text-secondary: #525252;
  --color-text-tertiary: #a3a3a3;

  --color-brand: #10b981;
  --color-brand-light: rgba(16, 185, 129, 0.1);
  --color-success: #10b981;
  --color-success-light: rgba(16, 185, 129, 0.1);
  --color-error: #ef4444;
  --color-error-light: rgba(239, 68, 68, 0.1);

  --font-sans: 'Inter', 'Noto Sans SC', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-serif: 'Noto Sans SC', sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', monospace;

  --header-height: 56px;
  --footer-height: 48px;
  --sidebar-width: 280px;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;

  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --duration-fast: 150ms;
  --duration-normal: 250ms;
}

[data-theme="dark"] {
  --color-bg-app: #0a0a0a;
  --color-bg-surface: #171717;
  --color-bg-surface-alt: #262626;
  --color-border: #262626;
  --color-border-hover: #404040;

  --color-text-primary: #fafafa;
  --color-text-secondary: #a3a3a3;
  --color-text-tertiary: #525252;

  --color-brand: #34d399;
  --color-brand-light: rgba(52, 211, 153, 0.15);
  --color-success: #34d399;
  --color-success-light: rgba(52, 211, 153, 0.15);
  --color-error: #f87171;
  --color-error-light: rgba(248, 113, 113, 0.15);
}

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

html, body {
  height: 100%;
}

body {
  font-family: var(--font-sans);
  background: var(--color-bg-app);
  color: var(--color-text-primary);
  line-height: 1.5;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button, select, input {
  font-family: inherit;
}

.app {
  display: flex;
  height: 100%;
  overflow: hidden;
}

.sidebar {
  width: var(--sidebar-width);
  background: var(--color-bg-surface);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
  transition: transform var(--duration-normal) var(--ease-out);
}

.sidebar-header {
  height: var(--header-height);
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.sidebar-title {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--color-text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.sidebar-close {
  display: none;
  background: none;
  border: none;
  color: var(--color-text-tertiary);
  cursor: pointer;
  padding: 4px;
}

.sidebar-close svg {
  width: 20px;
  height: 20px;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 40;
}

.nav-tree {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.nav-group {
  margin-bottom: 8px;
}

.nav-folder {
  user-select: none;
}

.nav-folder-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--color-text-secondary);
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: background var(--duration-fast);
}

.nav-folder-header:hover {
  background: var(--color-bg-surface-alt);
}

.nav-folder-icon {
  width: 16px;
  height: 16px;
  color: var(--color-text-tertiary);
  transition: transform var(--duration-fast);
  flex-shrink: 0;
}

.nav-folder.open > .nav-folder-header .nav-folder-icon {
  transform: rotate(90deg);
}

.nav-folder-children {
  display: none;
  padding-left: 20px;
}

.nav-folder.open > .nav-folder-children {
  display: block;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  font-size: 0.8125rem;
  color: var(--color-text-secondary);
  text-decoration: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--duration-fast);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-item:hover {
  background: var(--color-bg-surface-alt);
  color: var(--color-text-primary);
}

.nav-item.active {
  background: var(--color-brand-light);
  color: var(--color-brand);
  font-weight: 500;
}

.main-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.header {
  height: var(--header-height);
  background: var(--color-bg-surface);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  flex-shrink: 0;
  gap: 16px;
}

.header-left,
.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-center {
  flex: 1;
  display: flex;
  justify-content: center;
  min-width: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo-mark {
  font-size: 1.25rem;
  color: var(--color-brand);
  font-weight: 700;
}

.logo-text {
  font-size: 1rem;
  font-weight: 600;
  font-family: var(--font-serif);
  white-space: nowrap;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--color-text-secondary);
  cursor: pointer;
  padding: 8px;
  border-radius: var(--radius-sm);
  transition: all var(--duration-fast);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
}

.icon-btn:hover {
  background: var(--color-bg-surface-alt);
  color: var(--color-text-primary);
}

.icon-btn svg {
  width: 20px;
  height: 20px;
}

.sidebar-toggle {
  display: none;
}

.theme-toggle {
  background: transparent;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--color-text-secondary);
  transition: all var(--duration-fast);
}

.theme-toggle:hover {
  border-color: var(--color-text-tertiary);
  color: var(--color-text-primary);
}

.theme-toggle svg {
  width: 18px;
  height: 18px;
}

[data-theme="light"] .icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: none; }

.encoding-selector select {
  appearance: none;
  background: var(--color-bg-surface-alt);
  border: 1px solid var(--color-border);
  padding: 6px 28px 6px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.8125rem;
  color: var(--color-text-primary);
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8125rem;
  color: var(--color-text-tertiary);
  overflow: hidden;
}

.breadcrumb-link {
  color: var(--color-text-tertiary);
  text-decoration: none;
  white-space: nowrap;
  transition: color var(--duration-fast);
}

.breadcrumb-link:hover {
  color: var(--color-text-primary);
}

.breadcrumb-separator {
  color: var(--color-text-tertiary);
}

.breadcrumb-current {
  color: var(--color-text-primary);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.main-content {
  flex: 1;
  overflow-y: auto;
  background: var(--color-bg-app);
}

.view {
  padding: 24px;
  max-width: 1000px;
  margin: 0 auto;
  animation: fadeIn 0.3s var(--ease-out);
}

.view-header {
  margin-bottom: 24px;
}

.view-header h2 {
  font-family: var(--font-serif);
  font-size: 1.5rem;
  font-weight: 600;
}

.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 24px;
}

.library-card {
  background: var(--color-bg-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 32px 24px;
  cursor: pointer;
  transition: all var(--duration-normal) var(--ease-out);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  position: relative;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.02), 0 2px 4px -1px rgba(0, 0, 0, 0.02);
}

.library-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.08), 0 10px 10px -5px rgba(0, 0, 0, 0.02);
  border-color: var(--color-border-hover);
}

.library-card:hover .library-card-icon {
  transform: scale(1.1) rotate(-2deg);
}

.library-card-icon {
  width: 56px;
  height: 56px;
  margin-bottom: 20px;
  transition: transform var(--duration-normal) var(--ease-out);
  display: flex;
  align-items: center;
  justify-content: center;
}

.library-card-icon svg {
  width: 100%;
  height: 100%;
}

.library-card-icon.folder {
  color: var(--color-brand);
  filter: drop-shadow(0 4px 6px var(--color-brand-light));
}

.library-card-icon.file {
  color: var(--color-text-tertiary);
  padding: 6px;
}

.library-card-title {
  font-size: 1.0625rem;
  font-weight: 600;
  color: var(--color-text-primary);
  margin-bottom: 6px;
  font-family: var(--font-sans);
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.library-card-meta {
  font-size: 0.8125rem;
  color: var(--color-text-tertiary);
  font-weight: 500;
  letter-spacing: 0.01em;
}

.view-practice {
  padding: 0;
  max-width: none;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.practice-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 24px;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-bg-surface);
  flex-shrink: 0;
}

.practice-title {
  font-family: var(--font-serif);
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--color-text-primary);
}

.segments-container {
  flex: 1;
  overflow-y: auto;
  padding: 32px;
}

.segment {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding: 16px;
  margin-bottom: 8px;
  border-radius: var(--radius-md);
  transition: all var(--duration-fast);
  position: relative;
}

.segment:hover {
  background: var(--color-bg-surface);
}

.segment.active {
  background: var(--color-bg-surface);
  box-shadow: inset 0 0 0 2px var(--color-brand-light);
}

.segment.completed {
  opacity: 0.6;
}

.segment-title {
  font-family: var(--font-serif);
  font-weight: 600;
  color: var(--color-text-tertiary);
}

.segment-title.level-1 { font-size: 1.5rem; }
.segment-title.level-2 { font-size: 1.25rem; }
.segment-title.level-3 { font-size: 1.125rem; }
.segment-title.level-4 { font-size: 1rem; }

.segment-content {
  flex: 1;
  min-width: 0;
}

.segment-clause-number {
  font-weight: 600;
  color: var(--color-brand);
  margin-right: 8px;
}

.segment-text {
  font-family: var(--font-serif);
  font-size: 1.125rem;
  line-height: 2;
  display: inline;
}

.segment-score {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
  font-size: 0.75rem;
  min-width: 60px;
}

.segment-score-value {
  font-weight: 600;
  font-family: var(--font-mono);
  color: var(--color-text-tertiary);
}

.segment-score-value.good {
  color: var(--color-success);
}

.segment-score-value.bad {
  color: var(--color-error);
}

.segment-score-chars {
  color: var(--color-text-tertiary);
}

.segment-score-meta {
  display: flex;
  align-items: center;
  gap: 6px;
}

.segment-score-speed {
  font-family: var(--font-mono);
  font-weight: 500;
  color: var(--color-text-tertiary);
  opacity: 0;
  transform: translateX(8px);
}

.segment-score-speed.speed-appear {
  animation: speedReveal 0.4s var(--ease-out) forwards;
}

.segment-score-speed.speed-good {
  color: var(--color-success);
}

.segment-score-speed.speed-fast {
  color: var(--color-brand);
}

.segment-score-speed.speed-fast.speed-appear {
  animation: speedRevealFast 0.5s var(--ease-out) forwards;
}

@keyframes speedReveal {
  0% {
    opacity: 0;
    transform: translateX(8px);
  }
  100% {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes speedRevealFast {
  0% {
    opacity: 0;
    transform: translateX(8px) scale(1);
  }
  50% {
    opacity: 1;
    transform: translateX(0) scale(1.08);
  }
  100% {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
}

.char-wrapper {
  display: inline-block;
  position: relative;
  margin: 0 1px;
  vertical-align: baseline;
}

.char-hint {
  position: absolute;
  left: 50%;
  bottom: 100%;
  transform: translateX(-50%);
  font-size: 0.6875rem;
  font-family: var(--font-sans);
  color: var(--color-text-tertiary);
  line-height: 1;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
}

.char {
  display: inline-block;
  vertical-align: baseline;
  padding: 2px 3px;
  border-radius: 3px;
  transition: color 0.1s, background-color 0.1s;
}

.char.pending {
  color: var(--color-text-secondary);
}

.char.current {
  color: var(--color-text-primary);
  background: var(--color-brand-light);
  outline: 1px solid var(--color-brand);
}

.char.correct {
  color: var(--color-success);
}

.char.incorrect {
  color: var(--color-error);
  background: var(--color-error-light);
}

.hidden-input {
  position: fixed;
  opacity: 0;
  pointer-events: none;
  width: 1px;
  height: 1px;
}

.footer {
  height: var(--footer-height);
  background: var(--color-bg-surface);
  border-top: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.footer-stats {
  display: flex;
  align-items: center;
  gap: 24px;
  font-size: 0.8125rem;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.stat-label {
  color: var(--color-text-tertiary);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.stat-value {
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--color-text-primary);
}

.stat-value small {
  font-weight: 400;
  color: var(--color-text-tertiary);
}

.stat-divider {
  width: 1px;
  height: 16px;
  background: var(--color-border);
}

.stat-sep {
  color: var(--color-text-tertiary);
  margin: 0 2px;
}

.results-screen {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.results-card {
  background: var(--color-bg-surface);
  border-radius: var(--radius-lg);
  padding: 40px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
  text-align: center;
  animation: slideUp 0.4s var(--ease-out);
}

.results-header {
  margin-bottom: 24px;
}

.results-icon {
  font-size: 2.5rem;
  margin-bottom: 12px;
}

.results-title {
  font-family: var(--font-serif);
  font-size: 1.5rem;
  font-weight: 600;
}

.results-stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--color-bg-surface-alt);
  padding: 16px;
  border-radius: var(--radius-md);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-card .stat-value {
  font-size: 1.25rem;
}

.stat-card .stat-label {
  font-size: 0.6875rem;
}

.stat-unit {
  font-size: 0.75rem;
  font-weight: 400;
  color: var(--color-text-tertiary);
}

.results-actions {
  display: flex;
  gap: 12px;
}

.btn {
  flex: 1;
  padding: 12px 20px;
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: 0.875rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: none;
  transition: all var(--duration-fast);
}

.btn:active {
  transform: scale(0.98);
}

.btn-primary {
  background: var(--color-brand);
  color: white;
}

.btn-primary:hover {
  filter: brightness(1.1);
}

.btn-secondary {
  background: var(--color-bg-surface-alt);
  color: var(--color-text-primary);
}

.btn-secondary:hover {
  background: var(--color-border);
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 50;
    transform: translateX(-100%);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.1);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-overlay.open {
    display: block;
  }

  .sidebar-close {
    display: flex;
  }

  .sidebar-toggle {
    display: flex;
  }

  .header-center {
    display: none;
  }

  .view {
    padding: 16px;
  }

  .library-grid {
    grid-template-columns: 1fr;
  }

  .segments-container {
    padding: 16px;
  }

  .segment {
    padding: 12px;
    flex-direction: column;
    gap: 8px;
  }

  .segment-score {
    flex-direction: row;
    gap: 8px;
    align-items: center;
  }

  .segment-text {
    font-size: 1rem;
  }

  .results-card {
    padding: 24px;
  }

  .results-stats-grid {
    grid-template-columns: 1fr 1fr;
  }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(16px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

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

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-2px); }
  40%, 80% { transform: translateX(2px); }
}

@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

.char.current {
  animation: pulse 1.5s ease-in-out infinite;
}

.char.incorrect {
  animation: shake 0.3s ease-out;
}

.segment.active {
  animation: fadeIn 0.3s ease-out;
}

.library-card {
  animation: fadeIn 0.4s ease-out backwards;
}

.library-card:nth-child(1) { animation-delay: 0.05s; }
.library-card:nth-child(2) { animation-delay: 0.1s; }
.library-card:nth-child(3) { animation-delay: 0.15s; }
.library-card:nth-child(4) { animation-delay: 0.2s; }
.library-card:nth-child(5) { animation-delay: 0.25s; }
.library-card:nth-child(6) { animation-delay: 0.3s; }

.nav-folder-children {
  animation: fadeIn 0.2s ease-out;
}

.segment-score-value.good {
  animation: pop 0.3s ease-out;
}

::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--color-border-hover);
}

::selection {
  background: var(--color-brand-light);
  color: var(--color-brand);
}

.segment:focus-within {
  outline: none;
}

.practice-header {
  transition: background var(--duration-normal);
}

.sidebar {
  will-change: transform;
}

.results-screen {
  animation: fadeIn 0.2s ease-out;
}

[data-theme="dark"] .results-screen {
  background: rgba(0, 0, 0, 0.7);
}
