:root {
  --w: 1024px;
  --h: 768px;
  --bg-1: #efe6db; /* light sepia */
  --bg-2: #e3d6c8;
  --win: #f7f1ea;
  --tb-active: #8a5a3b;
  --tb-inactive: #b89a83;
  --bevel-light: #fff;
  --bevel-dark: #6b4e3a;
  --accent: #c07b3b;
  --accent-dark: #8a5a3b;
  --text: #2b1f18;
  --text-light: #5a4a3e;
  --shadow: rgba(0, 0, 0, 0.2);
}

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

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #1a1612;
  font-family: 'Courier Prime', 'Courier New', monospace;
  color: var(--text);
  overflow: hidden;
  position: relative;
  user-select: none;
}

/* Custom retro cursor */
body, a, button, .icon {
  cursor: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"><path fill="%238a5a3b" d="M2,2 L8,2 L2,8 Z"/><path fill="%23f7f1ea" d="M0,0 L6,0 L0,6 Z"/></svg>') 0 0, auto;
}

.desktop {
  width: var(--w);
  height: var(--h);
  background: linear-gradient(135deg, var(--bg-1), var(--bg-2));
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.8);
  position: relative;
  overflow: hidden;
  border: 6px solid var(--bevel-dark);
  image-rendering: pixelated;
  display: flex;
  flex-direction: column;
}

/* Wallpaper pattern */
.desktop::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: 
    repeating-linear-gradient(0deg, transparent, transparent 1px, rgba(0,0,0,0.01) 1px, rgba(0,0,0,0.01) 2px),
    repeating-linear-gradient(90deg, transparent, transparent 1px, rgba(0,0,0,0.01) 1px, rgba(0,0,0,0.01) 2px);
  pointer-events: none;
  opacity: 0.3;
}

/* Icons area */
.icons {
  padding: 24px;
  display: grid;
  grid-template-columns: repeat(auto-fill, 84px);
  grid-auto-rows: 84px;
  gap: 20px;
  align-content: start;
  flex: 1;
  overflow-y: auto;
}

.icon {
  width: 84px;
  height: 84px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  outline: none;
  padding: 8px;
  transition: all 0.1s ease;
  border-radius: 4px;
  position: relative;
}

.icon:focus {
  background-color: rgba(138, 90, 59, 0.1);
  outline: 1px dashed var(--accent);
}

.icon:hover {
  background-color: rgba(138, 90, 59, 0.08);
  transform: translateY(-2px);
}

.icon.selected {
  background-color: rgba(138, 90, 59, 0.15);
  outline: 1px dashed var(--accent);
}

.icon img {
  width: 48px;
  height: 48px;
  image-rendering: pixelated;
  display: block;
  margin-bottom: 6px;
  filter: sepia(0.8) contrast(1.1);
}

.icon span {
  display: block;
  font-size: 11px;
  color: var(--text);
  text-align: center;
  line-height: 1.2;
  font-family: 'VT323', monospace;
  text-shadow: 1px 1px 0 var(--bevel-light);
  word-break: break-word;
  padding: 0 2px;
}

/* Window chrome */
.window {
  position: absolute;
  left: 160px;
  top: 100px;
  width: 720px;
  min-height: 420px;
  background: var(--win);
  border-top: 2px solid var(--bevel-light);
  border-left: 2px solid var(--bevel-light);
  border-right: 2px solid var(--bevel-dark);
  border-bottom: 2px solid var(--bevel-dark);
  box-shadow: 6px 6px 0 var(--shadow);
  display: none;
  flex-direction: column;
  font-family: 'Courier Prime', 'Courier New', monospace;
  color: var(--text);
  overflow: hidden;
  resize: both;
  min-width: 400px;
  min-height: 300px;
  max-width: 90vw;
  max-height: 90vh;
  opacity: 0;
  transform: scale(0.95);
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.window.active {
  display: flex;
  z-index: 100;
  opacity: 1;
  transform: scale(1);
}

.window.loading {
  display: flex;
  opacity: 0.7;
  pointer-events: none;
}

.window.closing {
  opacity: 0;
  transform: scale(0.9);
  pointer-events: none;
}

.window.dragging {
  opacity: 0.9;
  cursor: move;
}

/* Loading overlay for windows */
.window-loading {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(247, 241, 234, 0.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
  font-family: 'VT323', monospace;
  font-size: 18px;
  color: var(--accent-dark);
  text-align: center;
  padding: 20px;
}

.window-loading-spinner {
  width: 40px;
  height: 40px;
  margin-bottom: 15px;
  position: relative;
}

.window-loading-spinner::before,
.window-loading-spinner::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  border: 3px solid transparent;
  border-top-color: var(--accent);
  animation: spin 1s linear infinite;
}

.window-loading-spinner::after {
  border-top-color: var(--accent-dark);
  animation: spin 0.5s linear infinite;
  width: 70%;
  height: 70%;
  top: 15%;
  left: 15%;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* Titlebar */
.titlebar {
  height: 32px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  gap: 8px;
  user-select: none;
  cursor: move;
  font-family: 'VT323', monospace;
  font-size: 16px;
  letter-spacing: 0.5px;
  color: #fff;
  position: relative;
}

.titlebar.active {
  background: linear-gradient(90deg, var(--tb-active), #a17252);
}

.titlebar.inactive {
  background: linear-gradient(90deg, var(--tb-inactive), #d4c2b1);
  color: var(--text);
}

.titlebar .title {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
}

.controls {
  display: flex;
  gap: 6px;
}

.ctrl-btn {
  width: 20px;
  height: 18px;
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid;
  border-color: var(--bevel-light) var(--bevel-dark) var(--bevel-dark) var(--bevel-light);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 12px;
  color: #fff;
  cursor: pointer;
  font-family: 'VT323', monospace;
  z-index: 10;
  position: relative;
}

.ctrl-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.ctrl-btn:active {
  border-color: var(--bevel-dark) var(--bevel-light) var(--bevel-light) var(--bevel-dark);
}

.ctrl-btn.close {
  color: #d44;
}

/* Toolbar under titlebar */
.toolbar {
  background: #f0e9df;
  padding: 6px 10px;
  display: flex;
  gap: 8px;
  align-items: center;
  border-bottom: 1px solid #d9c9b8;
  flex-wrap: wrap;
}

.toolbar button {
  background: var(--win);
  border: 1px solid;
  border-color: var(--bevel-light) var(--bevel-dark) var(--bevel-dark) var(--bevel-light);
  padding: 4px 10px;
  font-size: 13px;
  cursor: pointer;
  color: var(--text);
  font-family: 'VT323', monospace;
  font-size: 15px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.toolbar button:hover {
  background: #f7f1ea;
}

.toolbar button:active {
  border-color: var(--bevel-dark) var(--bevel-light) var(--bevel-light) var(--bevel-dark);
}

/* Content area */
.reader {
  display: flex;
  gap: 20px;
  height: calc(100% - 32px - 40px);
  overflow: hidden;
  flex: 1;
}

.sidebar {
  width: 160px;
  border-right: 1px dashed #d9c9b8;
  padding: 15px 10px;
  overflow-y: auto;
  background: #f5eee4;
}

.sidebar h3 {
  font-family: 'VT323', monospace;
  font-size: 18px;
  margin-bottom: 10px;
  color: var(--accent-dark);
}

.sidebar ul {
  list-style: none;
  padding-left: 5px;
}

.sidebar li {
  margin-bottom: 8px;
}

.sidebar button {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  font-family: 'Courier Prime', monospace;
  font-size: 14px;
  text-align: left;
  padding: 3px 5px;
  width: 100%;
  border-radius: 2px;
}

.sidebar button:hover {
  background: rgba(138, 90, 59, 0.1);
}

.sidebar button.active {
  background: rgba(138, 90, 59, 0.15);
  font-weight: bold;
}

.main {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background: linear-gradient(180deg, #fcf8f3, #f7f1ea);
}

/* Page container - emulate pages */
.page {
  background: #fcf8f3;
  padding: 25px 30px;
  margin-bottom: 25px;
  border: 1px solid #e6d9c9;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
  border-radius: 2px;
  max-width: 680px;
  margin: 0 auto 25px;
}

.page h1 {
  font-family: 'Press Start 2P', monospace;
  font-size: 20px;
  margin: 0 0 20px 0;
  color: var(--accent-dark);
  line-height: 1.4;
}

.page h2 {
  font-family: 'Press Start 2P', monospace;
  font-size: 16px;
  margin: 25px 0 15px;
  color: var(--accent-dark);
}

.page p {
  font-size: 16px;
  line-height: 1.7;
  color: var(--text);
  margin: 0 0 16px 0;
  text-align: justify;
}

.page .chapter-number {
  font-family: 'VT323', monospace;
  font-size: 20px;
  color: var(--accent);
  margin-bottom: 5px;
  letter-spacing: 1px;
}

.page .pixel-quote {
  font-family: 'VT323', monospace;
  font-size: 18px;
  border-left: 3px solid var(--accent);
  padding-left: 15px;
  margin: 20px 0;
  background-color: rgba(192, 123, 59, 0.08);
  padding: 12px 15px;
  color: var(--text-light);
}

/* Footer with pagination */
.footer {
  height: 36px;
  background: #f0e9df;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  border-top: 1px solid #d9c9b8;
  font-family: 'VT323', monospace;
  font-size: 16px;
}

.footer button {
  background: var(--win);
  border: 1px solid;
  border-color: var(--bevel-light) var(--bevel-dark) var(--bevel-dark) var(--bevel-light);
  padding: 3px 10px;
  font-family: 'VT323', monospace;
  font-size: 14px;
  cursor: pointer;
  color: var(--text);
}

.footer button:hover {
  background: #f7f1ea;
}

/* Taskbar */
.taskbar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 36px;
  background: linear-gradient(to bottom, #3b2f28, #2f241d);
  display: flex;
  align-items: center;
  padding: 0 10px;
  gap: 8px;
  color: #e6d9c9;
  border-top: 2px solid #5a4a3e;
  z-index: 1000;
}

.taskbar .start-btn {
  background: linear-gradient(to bottom, #6b5442, #4a3729);
  border: 2px solid;
  border-color: #9c846f #2f241d #2f241d #9c846f;
  padding: 4px 15px;
  font-family: 'VT323', monospace;
  font-size: 18px;
  font-weight: bold;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: #f7f1ea;
  min-width: 80px;
}

.taskbar .start-btn:hover {
  background: linear-gradient(to bottom, #7c6553, #5b483a);
}

.taskbar .start-btn:active {
  border-color: #2f241d #9c846f #9c846f #2f241d;
}

#openTasks {
  display: flex;
  gap: 6px;
  flex: 1;
  overflow-x: auto;
  padding: 5px 0;
}

.task {
  background: linear-gradient(to bottom, #5a4a3e, #3b2f28);
  border: 1px solid;
  border-color: #7d6b5c #2f241d #2f241d #7d6b5c;
  padding: 4px 12px;
  font-family: 'VT323', monospace;
  font-size: 14px;
  white-space: nowrap;
  cursor: pointer;
  color: #e6d9c9;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.task.active {
  background: linear-gradient(to bottom, #8a5a3b, #6b4e3a);
  border-color: #a17252 #5a3f2b #5a3f2b #a17252;
}

.clock {
  font-family: 'VT323', monospace;
  font-size: 16px;
  padding: 4px 10px;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid #5a4a3e;
  min-width: 140px;
  text-align: center;
}

/* Start Menu */
.start-menu {
  position: absolute;
  bottom: 36px;
  left: 10px;
  width: 280px;
  background: #f7f1ea;
  border: 2px solid;
  border-color: var(--bevel-light) var(--bevel-dark) var(--bevel-dark) var(--bevel-light);
  display: none;
  z-index: 2000;
  box-shadow: 4px 4px 0 var(--shadow);
}

.start-menu.active {
  display: block;
}

.start-menu-header {
  background: linear-gradient(90deg, var(--tb-active), #a17252);
  color: white;
  padding: 10px 15px;
  font-family: 'VT323', monospace;
  font-size: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.start-menu-items {
  padding: 10px 0;
}

.start-menu-item {
  padding: 10px 15px 10px 45px;
  font-family: 'VT323', monospace;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  position: relative;
}

.start-menu-item:hover {
  background: rgba(138, 90, 59, 0.15);
}

.start-menu-item i {
  position: absolute;
  left: 15px;
}

/* Sound Toggle */
.sound-toggle {
  position: absolute;
  top: 15px;
  right: 15px;
  background: var(--win);
  border: 2px solid;
  border-color: var(--bevel-dark) var(--bevel-light) var(--bevel-light) var(--bevel-dark);
  padding: 5px 12px;
  font-family: 'VT323', monospace;
  font-size: 16px;
  cursor: pointer;
  z-index: 1001;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}

.sound-toggle:hover {
  background: #f7f1ea;
}

/* Loading Screen */
.loading-screen {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--tb-active);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 10000;
  color: #f7f1ea;
  font-family: 'Press Start 2P', monospace;
  transition: opacity 0.5s;
}

.loading-text {
  font-size: 20px;
  margin-bottom: 30px;
  text-align: center;
  line-height: 1.5;
}

.loading-bar {
  width: 300px;
  height: 24px;
  border: 3px solid #f7f1ea;
  padding: 3px;
}

.loading-progress {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.3s;
}

/* Responsive */
@media (max-width: 1100px) {
  .desktop {
    width: 100%;
    height: 100vh;
    border-radius: 0;
    border: none;
  }
  
  .window {
    left: 50%;
    top: 50px;
    transform: translateX(-50%);
    width: 95vw;
    height: calc(100vh - 140px);
    max-width: 95vw;
    max-height: calc(100vh - 140px);
  }
  
  .icons {
    padding: 15px;
    grid-template-columns: repeat(auto-fill, 80px);
    grid-auto-rows: 80px;
    gap: 15px;
  }
  
  .icon {
    width: 80px;
    height: 80px;
  }
  
  .icon img {
    width: 42px;
    height: 42px;
  }
  
  .reader {
    flex-direction: column;
  }
  
  .sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px dashed #d9c9b8;
    padding: 10px;
    max-height: 150px;
  }
}

@media (max-width: 768px) {
  .toolbar {
    padding: 5px;
    gap: 5px;
  }
  
  .toolbar button {
    padding: 3px 6px;
    font-size: 13px;
  }
  
  .taskbar {
    height: 44px;
    flex-wrap: wrap;
    justify-content: space-between;
  }
  
  #openTasks {
    order: 3;
    width: 100%;
    margin-top: 5px;
  }
  
  .clock {
    min-width: 120px;
  }
}

/* Accessibility */
button:focus, .icon:focus, .task:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

::-webkit-scrollbar {
  width: 12px;
}

::-webkit-scrollbar-track {
  background: #f0e9df;
}

::-webkit-scrollbar-thumb {
  background: #c9b8a7;
  border: 2px solid #f0e9df;
}

::-webkit-scrollbar-thumb:hover {
  background: #b89a83;
}