/* --- CSS Variables and Base Styles --- */
:root {
  --bg-dark: #121417;
  --bg-panel: #1a1d21;
  --border-color: #2c3038;
  --text-primary: #e2e2e2;
  --text-secondary: #8c929c;
  --accent-primary: #00aaff;
  --accent-primary-hover: #33bbff;
  --accent-danger: #ff4757;
  --font-main: 'Roboto', sans-serif;
  --sidebar-width: 280px; /* 侧边栏宽度 */
  --gap: 20px; /* 间距 */
}

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

body {
  background-color: var(--bg-dark);
  color: var(--text-primary);
  font-family: var(--font-main);
  min-height: 100vh;
  margin: 0;
  overflow-y: auto; /* Allow vertical scrolling */
}

/* 页面整体外边框 */
.page-wrapper {
  width: 100%;
  min-height: 100%;
  padding: 10px;
  border: 2px solid var(--border-color);
  border-radius: 16px;
  background: linear-gradient(
    135deg,
    rgba(44, 48, 56, 0.1) 0%,
    rgba(44, 48, 56, 0.05) 50%,
    rgba(44, 48, 56, 0.1) 100%
  );
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.1),
    0 0 20px rgba(0, 170, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}

.page-wrapper.layout-horizontal {
  height: 100vh;
}

.page-wrapper.layout-vertical {
  height: auto;
  align-items: flex-start;
}

.container {
  width: 100%;
  height: 100%;
  display: flex;
  gap: var(--gap);
  align-items: stretch;
  justify-content: center;
}

/* --- Layout Modes --- */

/* Horizontal Layout (Sidebars Left/Right) */
.container.layout-horizontal {
  flex-direction: row;
}

.layout-horizontal .game-main {
  height: 100%;
  flex: 0 0 auto; /* Let aspect ratio define width based on height */
}

.layout-horizontal .sidebar {
  flex: 1 0 var(--sidebar-width); /* Allow sidebars to grow from their base width */
  height: 100%;
}

.layout-horizontal .sidebar .stats-panel,
.layout-horizontal .sidebar .controls {
  flex-direction: column;
}

.layout-horizontal .sidebar .stats-grid {
  flex-direction: column;
  gap: 0;
  width: 100%;
}

.layout-horizontal .sidebar .stat-item {
  flex-direction: row;
  justify-content: space-between;
  width: 100%;
  padding: 12px 0;
  border: none;
  border-bottom: 1px solid var(--border-color);
  border-radius: 0;
}
.layout-horizontal .sidebar .stat-item:last-child {
  border-bottom: none;
}

/* Vertical Layout (Sidebars Top/Bottom) */
.container.layout-vertical {
  flex-direction: column;
}

.layout-vertical .game-main {
  width: 100%;
  flex: 0 1 auto; /* Allow shrinking */
  min-height: 0;
}

.layout-vertical .sidebar {
  width: 100%;
  height: auto;
}

/* --- Vertical Layout Sidebar Overrides --- */

/* Top Sidebar (#sidebar-1) */
.layout-vertical #sidebar-1 {
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 15px;
}

.layout-vertical #sidebar-1 .game-header {
  text-align: left;
  margin: 0; /* Reset default margins */
}

.layout-vertical #sidebar-1 .controls {
  width: auto; /* Override 100% width */
}

.layout-vertical #sidebar-1 .controls button {
  width: auto; /* Let padding define width */
  flex-grow: 0; /* Don't expand */
  padding: 10px 24px; /* Adjust padding for vertical view */
}

.layout-vertical #sidebar-1 .instructions {
  display: none; /* Hide instructions in vertical layout */
}

/* Bottom Sidebar (#sidebar-2) */
.layout-vertical #sidebar-2 {
  justify-content: center;
}

.layout-vertical #sidebar-2 .stats-panel {
  flex-direction: column;
  align-items: center;
  gap: 15px;
}

.layout-vertical #sidebar-2 .stats-grid {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  gap: 15px;
}

/* --- Components --- */

.sidebar {
  background-color: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 20px;
  display: flex;
  position: relative;
  z-index: 1;
  overflow: hidden;
  flex-direction: column;
  justify-content: space-between;
  flex: 0 0 auto; /* Do not grow or shrink */
  overflow-y: auto;
}

.game-main {
  background-color: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 20px;
  display: flex;
  position: relative;
  z-index: 1;
  overflow: hidden;
  justify-content: center;
  align-items: center;
  aspect-ratio: 1 / 1;
}

#sidebar-1 {
  align-items: center;
}

#gameCanvas {
  display: block; /* 移除 flex 布局可能产生的额外空间 */
  object-fit: contain; /* 保证比例的同时填满容器 */
  max-width: 100%;
  max-height: 100%;
  border: 1px solid var(--border-color);
  border-radius: 4px;
}

.game-over-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(18, 20, 23, 0.85);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  text-align: center;
  z-index: 100;
  animation: fadeIn 0.5s ease-in-out;
}

.game-over-overlay h2 {
  font-size: 2.5rem;
  margin-bottom: 1rem;
  color: var(--accent-danger);
}

.game-over-overlay p {
  font-size: 1.2rem;
  margin-bottom: 2rem;
}

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

/* --- Typography and Content --- */
h1 {
  font-size: clamp(18px, 3vw, 28px);
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: 1px;
  text-align: center;
  margin: 0 0 8px 0;
}

.subtitle {
  font-size: clamp(11px, 1.5vw, 14px);
  font-weight: 300;
  color: var(--accent-primary);
  text-align: center;
  margin: 0 0 16px 0;
}

h2 {
  font-size: clamp(16px, 2.5vw, 20px);
  font-weight: 700;
  color: var(--text-primary);
  margin: 0 0 16px 0;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 8px;
  width: 100%;
  text-align: center;
}

.instructions {
  font-size: 0.85rem;
  color: var(--text-secondary);
  text-align: center;
}
.instructions p {
  margin-bottom: 5px;
}
.instructions strong {
  color: var(--text-primary);
}

/* --- Controls --- */
.controls {
  display: flex;
  width: 100%;
  gap: 10px;
  align-items: center;
}

button {
  flex-grow: 1;
  padding: 12px 20px;
  font-size: 1rem;
  font-weight: 700;
  color: #fff;
  background-color: var(--accent-primary);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s;
  text-align: center;
  width: 100%;
}

button:hover {
  background-color: var(--accent-primary-hover);
}

button:active {
  transform: translateY(1px);
}

#btnRestart {
  background-color: var(--accent-danger);
}
#btnRestart:hover {
  background-color: #ff6b81;
}

/* --- Stats Panel --- */
.stats-panel {
  width: 100%;
}

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

.stat-item .label {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

.stat-item .value {
  color: var(--text-primary);
  font-size: 1.1rem;
  font-weight: 700;
}

/* Fallback for no-script */
.no-script {
  display: none;
  padding: 1rem;
  background: var(--accent-danger);
  color: white;
  text-align: center;
}

html:not(.js-enabled) .no-script {
  display: block;
}
