/* ========================================
       TruthForge - Logic Gate Simulator
       Build, Test, Learn Boolean Logic
    ======================================== */

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

:root {
	--true-color: #22c55e;
	--true-glow: rgba(34, 197, 94, 0.5);
	--false-color: #ef4444;
	--false-glow: rgba(239, 68, 68, 0.5);
	--wire-active: #22c55e;
	--wire-inactive: #475569;
	--bg-primary: #0f172a;
	--bg-secondary: #1e293b;
	--bg-tertiary: #334155;
	--bg-card: #1e293b;
	--text-primary: #f8fafc;
	--text-secondary: #94a3b8;
	--text-muted: #64748b;
	--accent-blue: #3b82f6;
	--accent-purple: #8b5cf6;
	--accent-orange: #f97316;
	--accent-cyan: #06b6d4;
	--border-color: #334155;
	--gate-and: #3b82f6;
	--gate-or: #8b5cf6;
	--gate-not: #f97316;
	--gate-xor: #06b6d4;
	--gate-nand: #ec4899;
	--gate-nor: #14b8a6;
}

body {
	font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
	background: var(--bg-primary);
	color: var(--text-primary);
	min-height: 100vh;
	overflow-x: hidden;
}

/* Header */
.header {
	background: var(--bg-secondary);
	border-bottom: 1px solid var(--border-color);
	padding: 16px 24px;
	display: flex;
	justify-content: space-between;
	align-items: center;
	position: sticky;
	top: 0;
	z-index: 100;
}

.logo {
	display: flex;
	align-items: center;
	gap: 12px;
}

.logo-icon {
	font-size: 1.8rem;
}

.logo-text {
	font-size: 1.5rem;
	font-weight: 700;
	background: linear-gradient(135deg, var(--accent-blue), var(--accent-purple));
	-webkit-background-clip: text;
	-webkit-text-fill-color: transparent;
	background-clip: text;
}

.logo-subtitle {
	font-size: 0.75rem;
	color: var(--text-muted);
	margin-top: 2px;
}

.header-actions {
	display: flex;
	gap: 10px;
}

.header-btn {
	padding: 10px 18px;
	background: var(--bg-tertiary);
	border: 1px solid var(--border-color);
	border-radius: 8px;
	color: var(--text-primary);
	font-size: 0.85rem;
	font-weight: 500;
	cursor: pointer;
	transition: all 0.2s ease;
	display: flex;
	align-items: center;
	gap: 6px;
}

.header-btn:hover {
	background: var(--accent-blue);
	border-color: var(--accent-blue);
}

.header-btn.danger:hover {
	background: var(--false-color);
	border-color: var(--false-color);
}

/* Main Layout */
.main-layout {
	display: grid;
	grid-template-columns: 280px 1fr 320px;
	min-height: calc(100vh - 65px);
}

/* Sidebar - Gates Palette */
.sidebar {
	background: var(--bg-secondary);
	border-right: 1px solid var(--border-color);
	padding: 20px;
	overflow-y: auto;
}

.sidebar-title {
	font-size: 0.8rem;
	font-weight: 600;
	color: var(--text-muted);
	text-transform: uppercase;
	letter-spacing: 1px;
	margin-bottom: 16px;
}

.gates-palette {
	display: flex;
	flex-direction: column;
	gap: 10px;
}

.gate-item {
	background: var(--bg-primary);
	border: 2px solid var(--border-color);
	border-radius: 12px;
	padding: 14px;
	cursor: grab;
	transition: all 0.2s ease;
	display: flex;
	align-items: center;
	gap: 12px;
}

.gate-item:hover {
	border-color: var(--accent-blue);
	transform: translateX(5px);
}

.gate-item:active {
	cursor: grabbing;
}

.gate-item.selected {
	border-color: var(--accent-purple);
	box-shadow: 0 0 20px rgba(139, 92, 246, 0.3);
}

.gate-symbol {
	width: 44px;
	height: 44px;
	border-radius: 10px;
	display: flex;
	align-items: center;
	justify-content: center;
	font-weight: 700;
	font-size: 0.75rem;
	color: white;
}

.gate-symbol.and {
	background: var(--gate-and);
}

.gate-symbol.or {
	background: var(--gate-or);
}

.gate-symbol.not {
	background: var(--gate-not);
}

.gate-symbol.xor {
	background: var(--gate-xor);
}

.gate-symbol.nand {
	background: var(--gate-nand);
}

.gate-symbol.nor {
	background: var(--gate-nor);
}

.gate-symbol.input {
	background: var(--true-color);
}

.gate-symbol.output {
	background: var(--text-muted);
}

.gate-info {
	flex: 1;
}

.gate-name {
	font-weight: 600;
	font-size: 0.95rem;
	margin-bottom: 2px;
}

.gate-desc {
	font-size: 0.75rem;
	color: var(--text-muted);
}

/* Divider */
.sidebar-divider {
	height: 1px;
	background: var(--border-color);
	margin: 20px 0;
}

/* Preset Circuits */
.presets-section {
	margin-top: 10px;
}

.preset-btn {
	width: 100%;
	padding: 12px 14px;
	background: var(--bg-primary);
	border: 1px solid var(--border-color);
	border-radius: 8px;
	color: var(--text-primary);
	font-size: 0.85rem;
	text-align: left;
	cursor: pointer;
	transition: all 0.2s ease;
	margin-bottom: 8px;
	display: flex;
	align-items: center;
	gap: 10px;
}

.preset-btn:hover {
	background: var(--bg-tertiary);
	border-color: var(--accent-blue);
}

/* Canvas Area */
.canvas-area {
	background: var(--bg-primary);
	position: relative;
	overflow: hidden;
	background-image: radial-gradient(
		circle,
		var(--bg-tertiary) 1px,
		transparent 1px
	);
	background-size: 20px 20px;
}

.canvas-container {
	width: 100%;
	height: 100%;
	position: relative;
}

#circuitCanvas {
	position: absolute;
	top: 0;
	left: 0;
	pointer-events: none;
}

/* Circuit Elements */
.circuit-element {
	position: absolute;
	background: var(--bg-card);
	border: 2px solid var(--border-color);
	border-radius: 12px;
	padding: 12px;
	cursor: move;
	transition: box-shadow 0.2s ease, border-color 0.2s ease;
	user-select: none;
	min-width: 100px;
}

.circuit-element:hover {
	border-color: var(--accent-blue);
}

.circuit-element.selected {
	border-color: var(--accent-purple);
	box-shadow: 0 0 25px rgba(139, 92, 246, 0.4);
}

.circuit-element.dragging {
	opacity: 0.8;
	z-index: 1000;
}

.element-header {
	display: flex;
	align-items: center;
	gap: 8px;
	margin-bottom: 10px;
}

.element-type {
	width: 28px;
	height: 28px;
	border-radius: 6px;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 0.65rem;
	font-weight: 700;
	color: white;
}

.element-label {
	font-size: 0.8rem;
	font-weight: 600;
	flex: 1;
}

.element-delete {
	width: 22px;
	height: 22px;
	border-radius: 4px;
	background: transparent;
	border: none;
	color: var(--text-muted);
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 0.9rem;
	transition: all 0.2s;
}

.element-delete:hover {
	background: var(--false-color);
	color: white;
}

/* Ports */
.element-ports {
	display: flex;
	justify-content: space-between;
	align-items: center;
	gap: 20px;
}

.ports-group {
	display: flex;
	flex-direction: column;
	gap: 8px;
}

.port {
	display: flex;
	align-items: center;
	gap: 6px;
	font-size: 0.7rem;
	color: var(--text-secondary);
}

.port-dot {
	width: 14px;
	height: 14px;
	border-radius: 50%;
	background: var(--bg-tertiary);
	border: 2px solid var(--wire-inactive);
	cursor: pointer;
	transition: all 0.2s ease;
}

.port-dot:hover {
	transform: scale(1.3);
	border-color: var(--accent-blue);
}

.port-dot.active {
	background: var(--true-color);
	border-color: var(--true-color);
	box-shadow: 0 0 10px var(--true-glow);
}

.port-dot.connecting {
	animation: pulse-port 0.5s infinite;
}

@keyframes pulse-port {
	0%,
	100% {
		transform: scale(1);
	}

	50% {
		transform: scale(1.4);
	}
}

.input-ports .port {
	flex-direction: row;
}

.output-ports .port {
	flex-direction: row-reverse;
}

/* Input Toggle */
.input-toggle {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 8px;
}

.toggle-switch {
	width: 60px;
	height: 32px;
	background: var(--false-color);
	border-radius: 16px;
	cursor: pointer;
	position: relative;
	transition: all 0.3s ease;
}

.toggle-switch.on {
	background: var(--true-color);
}

.toggle-switch::after {
	content: "";
	position: absolute;
	width: 26px;
	height: 26px;
	background: white;
	border-radius: 50%;
	top: 3px;
	left: 3px;
	transition: all 0.3s ease;
}

.toggle-switch.on::after {
	left: 31px;
}

.toggle-label {
	font-size: 0.85rem;
	font-weight: 600;
}

.toggle-label.true-label {
	color: var(--true-color);
}

.toggle-label.false-label {
	color: var(--false-color);
}

/* Output Display */
.output-display {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 8px;
}

.output-bulb {
	width: 50px;
	height: 50px;
	border-radius: 50%;
	background: var(--bg-tertiary);
	border: 3px solid var(--wire-inactive);
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 1.5rem;
	transition: all 0.3s ease;
}

.output-bulb.on {
	background: var(--true-color);
	border-color: var(--true-color);
	box-shadow: 0 0 30px var(--true-glow), 0 0 60px var(--true-glow);
}

.output-value {
	font-size: 0.85rem;
	font-weight: 600;
}

/* Right Panel - Truth Table & Info */
.right-panel {
	background: var(--bg-secondary);
	border-left: 1px solid var(--border-color);
	padding: 20px;
	overflow-y: auto;
}

.panel-section {
	margin-bottom: 24px;
}

.panel-title {
	font-size: 0.8rem;
	font-weight: 600;
	color: var(--text-muted);
	text-transform: uppercase;
	letter-spacing: 1px;
	margin-bottom: 14px;
	display: flex;
	align-items: center;
	gap: 8px;
}

/* Circuit Stats */
.circuit-stats {
	display: grid;
	grid-template-columns: repeat(2, 1fr);
	gap: 10px;
}

.stat-card {
	background: var(--bg-primary);
	border-radius: 10px;
	padding: 14px;
	text-align: center;
}

.stat-value {
	font-size: 1.5rem;
	font-weight: 700;
	color: var(--accent-blue);
}

.stat-label {
	font-size: 0.7rem;
	color: var(--text-muted);
	margin-top: 4px;
}

/* Truth Table */
.truth-table-container {
	background: var(--bg-primary);
	border-radius: 12px;
	overflow: hidden;
}

.truth-table {
	width: 100%;
	border-collapse: collapse;
	font-size: 0.8rem;
}

.truth-table th,
.truth-table td {
	padding: 10px 12px;
	text-align: center;
	border-bottom: 1px solid var(--border-color);
}

.truth-table th {
	background: var(--bg-tertiary);
	font-weight: 600;
	color: var(--text-secondary);
	font-size: 0.75rem;
	text-transform: uppercase;
	letter-spacing: 0.5px;
}

.truth-table td {
	font-family: "Consolas", monospace;
}

.truth-table tr:hover td {
	background: var(--bg-tertiary);
}

.truth-table .val-true {
	color: var(--true-color);
	font-weight: 600;
}

.truth-table .val-false {
	color: var(--false-color);
	font-weight: 600;
}

.truth-table tr.current-row td {
	background: rgba(59, 130, 246, 0.2);
}

/* Gate Reference */
.gate-reference {
	display: flex;
	flex-direction: column;
	gap: 10px;
}

.ref-item {
	background: var(--bg-primary);
	border-radius: 10px;
	padding: 12px;
	display: flex;
	align-items: center;
	gap: 12px;
}

.ref-symbol {
	width: 36px;
	height: 36px;
	border-radius: 8px;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 0.65rem;
	font-weight: 700;
	color: white;
}

.ref-info {
	flex: 1;
}

.ref-name {
	font-weight: 600;
	font-size: 0.85rem;
	margin-bottom: 2px;
}

.ref-formula {
	font-family: "Consolas", monospace;
	font-size: 0.75rem;
	color: var(--text-muted);
}

/* Tooltips */
.tooltip {
	position: absolute;
	background: var(--bg-tertiary);
	color: var(--text-primary);
	padding: 8px 12px;
	border-radius: 6px;
	font-size: 0.75rem;
	pointer-events: none;
	z-index: 1000;
	white-space: nowrap;
	box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

/* Empty State */
.empty-state {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	text-align: center;
	color: var(--text-muted);
}

.empty-icon {
	font-size: 4rem;
	margin-bottom: 16px;
	opacity: 0.5;
}

.empty-title {
	font-size: 1.2rem;
	font-weight: 600;
	margin-bottom: 8px;
	color: var(--text-secondary);
}

.empty-desc {
	font-size: 0.9rem;
	max-width: 300px;
}

/* Keyboard Shortcuts */
.shortcuts-panel {
	background: var(--bg-primary);
	border-radius: 10px;
	padding: 14px;
}

.shortcut-item {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 6px 0;
	font-size: 0.8rem;
}

.shortcut-key {
	background: var(--bg-tertiary);
	padding: 4px 8px;
	border-radius: 4px;
	font-family: "Consolas", monospace;
	font-size: 0.7rem;
}

/* Responsive */
@media (max-width: 1200px) {
	.main-layout {
		grid-template-columns: 240px 1fr 280px;
	}
}

@media (max-width: 900px) {
	.main-layout {
		grid-template-columns: 1fr;
		grid-template-rows: auto 1fr auto;
	}

	.sidebar {
		border-right: none;
		border-bottom: 1px solid var(--border-color);
	}

	.right-panel {
		border-left: none;
		border-top: 1px solid var(--border-color);
	}

	.gates-palette {
		flex-direction: row;
		flex-wrap: wrap;
	}

	.gate-item {
		flex: 1;
		min-width: 140px;
	}

	.canvas-area {
		min-height: 400px;
	}
}

/* Animations */
@keyframes pop-in {
	0% {
		transform: scale(0);
		opacity: 0;
	}

	70% {
		transform: scale(1.1);
	}

	100% {
		transform: scale(1);
		opacity: 1;
	}
}

.circuit-element.new {
	animation: pop-in 0.3s ease;
}

/* Wire Animation */
@keyframes flow {
	0% {
		stroke-dashoffset: 20;
	}

	100% {
		stroke-dashoffset: 0;
	}
}

.wire-active {
	stroke-dasharray: 10, 10;
	animation: flow 0.5s linear infinite;
}